标签:
就是,用手左右滑动页面
首先先在layout中配置好各个页面,我配置了新建的六个图片页面。
然后新建main.xml文件,
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:id="@+id/pager" ></android.support.v4.view.ViewPager> </LinearLayout>
然后去主文件
package com.example.deemo; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.view.View; public class MainActivity extends Activity { private List<View>viewList;//初始化 private ViewPager pager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); viewList=new ArrayList<View>(); //通过view对象作为ViewPager的数据源 View view1=View.inflate(this, R.layout.view1, null); View view2=View.inflate(this, R.layout.view2, null); View view3=View.inflate(this, R.layout.view3, null); View view4=View.inflate(this, R.layout.view4, null); View view5=View.inflate(this, R.layout.view5, null); View view6=View.inflate(this, R.layout.view6, null); viewList.add(view1); viewList.add(view2); viewList.add(view3); viewList.add(view4); viewList.add(view5); viewList.add(view6); pager=(ViewPager) findViewById(R.id.pager);//初始化viewpager MyPagerAdapter adapter=new MyPagerAdapter(viewList);//创建适配器 pager.setAdapter(adapter);//加载适配器 } }
接着新建class MyPagerAdapter的java文件,配置页卡
package com.example.deemo; import java.util.List; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; public class MyPagerAdapter extends PagerAdapter{ private List<View>viewList; public MyPagerAdapter(List<View>viewList){ this.viewList=viewList; } //返回的是页卡的数量 @Override public int getCount() { // TODO Auto-generated method stub return viewList.size(); } //判断当前view是否来自对象 @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0==arg1; } //实例化一个页卡 @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(viewList.get(position)); return viewList.get(position); } //销毁一个页卡 @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(viewList.get(position)); } }
标签:
原文地址:http://www.cnblogs.com/webgavin/p/5728495.html