1 ViewPager + PagerAdapter
- PagerAdapter
/**
* Created by louisgeek on 2016/3/23.
*/
public class MyPagerAdapter extends PagerAdapter {
private List<View> views;
public MyPagerAdapter(List<View> views) {
this.views = views;
}
@Override
public int getCount() {
return views.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// return super.instantiateItem(container, position);
View view=views.get(position);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
//super.destroyItem(container, position, object);
container.removeView(views.get(position));
}
@Override
public CharSequence getPageTitle(int position) {
//return super.getPageTitle(position);
return "标题"+position;
}
}
- 设置 ViewPager
ViewPager viewPager= (ViewPager) findViewById(R.id.id_view_pager);
MyPagerAdapter myPagerAdapter=new MyPagerAdapter(views);
viewPager.setAdapter(myPagerAdapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
2 ViewPager + FragmentPagerAdapter + Fragment
- FragmentPagerAdapter
/**
* Created by louisgeek on 2016/8/1.
* 不保留实例
* 只销毁里面的View
*/
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private List<Pair<String,Fragment>> mFragmentPair;
public MyFragmentPagerAdapter(FragmentManager fm,List<Pair<String,Fragment>> mFragmentPair) {
super(fm);
this.mFragmentPair = mFragmentPair;
}
@Override
public Fragment getItem(int position) {
return mFragmentPair.get(position).second;
}
@Override
public int getCount() {
return mFragmentPair.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentPair.get(position).first;
}
}
3 ViewPager + FragmentStatePagerAdapter + Fragment
- FragmentStatePagerAdapter
/**
* Created by louisgeek on 2016/8/1.
* 保留实例
*/
public class MyFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
private List<Pair<String,Fragment>> mFragmentPair;
public MyFragmentStatePagerAdapter(FragmentManager fm, List<Pair<String,Fragment>> mFragmentPair) {
super(fm);
this.mFragmentPair = mFragmentPair;
}
@Override
public Fragment getItem(int position) {
return mFragmentPair.get(position).second;
}
/**
* adapter.notifyDataSetChanged()的时候,ViewPager会去查询到底哪个位置有改变,
* return PagerAdapter.POSITION_NONE; 就是告诉所有的View已经变化,从而达到刷新的目的
* @param object
* @return
*/
@Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
// return super.getItemPosition(object);
}
@Override
public int getCount() {
return mFragmentPair.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentPair.get(position).first;
}
}
小结:
1.FragmentPagerAdapter和FragmentStatePagerAdapter 都继承PagerAdapter
2.使用FragmentPagerAdapter切换后不会销毁Fragment实例,只会销毁Fragment 的View,不会走onDestroy和onDetach,而使用FragmentStatePagerAdapter切换后会走onDestroy和onDetach ,由于FragmentStatePagerAdapter会保存了ArrayList<Fragment.SavedState> mSavedState和ArrayList<Fragment> mFragments,就是对Fragment实例列表和Fragment状态列表进行了保存,但是在切换后直接remove掉Fragment,也将mFragments中对应的置空,所以不能直接恢复状态,不过另外可以在 onSaveInstance 等相关方法中来进行数据的恢复
3.当页面数量较少的,切换来回频繁的情况可以使用FragmentPagerAdapter,页面数量较多的时候推荐使用FragmentStatePagerAdapter,但是需要自行处理下数据保存恢复
4.FragmentPagerAdapter和FragmentStatePagerAdapter最大的区别就是 instantiateItem 和 destroyItem 实现的不一样,(所以如果重写这2个方法后 区别比较就失去本身的意义了)
5.Android 总结7:ViewPager中Fragment滑动切换 数据保存实现方式 http://www.jianshu.com/p/7af21f5a7d30
参考资料:
FragmentPagerAdapter vs FragmentStatePagerAdapter
http://www.jianshu.com/p/3601da073ef8
Fragment 知识梳理(4) - FragmentPagerAdapter 和 FragmentStatePagerAdapter 解析
http://www.jianshu.com/p/8ac5152b2e06
ViewPager使用详解(一):PagerAdapter
http://www.jianshu.com/p/6fabf634d5bd
网友评论