效果图
在上文中提到了ViewPager有三种适配器:PagerAdapter、FragmentPagerAdapter、FragmentStatePagerAdapter。并使用PagerAdapter做了个导航栏。下面贴出FragmentPagerAdapter和FragmentStatePagerAdapter的使用方法。
FragmentPagerAdapter要点:
- MainActivity的布局文件中,引入ViewPager组件标签
- 自定义4个页面布局文件和4个fragemnt类,继承自Fragment
- 定义一个MyFragmentPagerAdapter适配器,继承FragmentPagerAdapter
- MainActivity中,将4个fragemnt加入到List。再把list传给MyFragmentPagerAdapter适配器
- ViewPager绑定适配器
详细源码
1、activity_main.xml,引入ViewPager标签,安卓3.0以下,需要使用v4兼容包
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vpager">
<android.support.v4.view.PagerTabStrip
android:id="@+id/pagerTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.v4.view.ViewPager>
</RelativeLayout>
2、4个页卡的布局文件。这里只贴出来一个page_one.xml,其他3个一样
<?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" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00"
android:gravity="center"
android:text="第一页"/>
</LinearLayout>
3、4个自定义的fragment,都是一样的
public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.page_one, container, false);
}
}
4、MyFragmentPagerAdapter适配器,继承自FragmentPagerAdapter。
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
private ArrayList<Fragment> fragmentList;
private ArrayList<String> titleList;
public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragmentList,ArrayList<String> titleList) {
super(fm);
this.fragmentList = fragmentList;
this.titleList = titleList;
}
@Override
public android.support.v4.app.Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return fragmentList.get(arg0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return titleList.get(position);
}
}
5、MainActivity。需要注意,如果前面使用的适配器import的是v4兼容包,那么这里MainActivity继承的是FragmentActivity
public class MainActivity extends FragmentActivity {
private ViewPager vpager;
private ArrayList<Fragment> fragmentList;
private ArrayList<String> titleList;
private MyFragmentPagerAdapter myFragmentPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vpager = (ViewPager) findViewById(R.id.vpager);
fragmentList = new ArrayList<Fragment>();
titleList = new ArrayList<String>();
fragmentList.add(new Fragment1());
fragmentList.add(new Fragment2());
fragmentList.add(new Fragment3());
fragmentList.add(new Fragment4());
titleList.add("第一页");
titleList.add("第二页");
titleList.add("第三页");
titleList.add("第四页");
myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),fragmentList,titleList);
vpager.setAdapter(myFragmentPagerAdapter);
}
}
补充
- FragmentStatePagerAdapter使用和FragmenPagerAdapter一样,只不过在自定义的适配器中,前者要额外重写destroyItem和instantiateItem这两个函数。
- MainActivity实现OnPageChangeListener接口,即可对ViewPager进行监听。