Android 中 FragmentPagerAdapter 与 FragmentStatePagerAdapter的区别

1、博文介绍

  • ViewPager防止Fragment销毁以及取消Fragment的预加载

  • FragmentPagerAdapter与FragmentStatePagerAdapter区别

  • FragmentStatePagerAdapter和FragmentPagerAdapter

  • 更新及替换ViewPager中的Fragment 遇到的坑

2、FragmentPagerAdapter中设置不让Fragment视图重新创建

Android 中 FragmentPagerAdapter 与 FragmentStatePagerAdapter的区别_第1张图片
image.png

Android 中 FragmentPagerAdapter 与 FragmentStatePagerAdapter的区别_第2张图片
image.png
FragmentPagerAdapter完整代码如下

/**
 * 主页ViewPager适配器
 * Created by _Smile on 2017/5/9.
 */
public class TabFragmentPagerAdapter extends FragmentPagerAdapter {

    private FragmentManager mFragmentManager;

    //保存每个Fragment的Tag,刷新页面的依据
    protected SparseArray tags = new SparseArray<>();

    //继承FragmentPagerAdapter类 ,并自定义的构造器
    private List fragments;
    public TabFragmentPagerAdapter(FragmentManager fm, List fragments) {
        super(fm);
        this.fragments =fragments;
        mFragmentManager = fm;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public long getItemId(int position) {
        return fragments.get(position).hashCode();
    }

    @Override
    public int getItemPosition(Object object) {
        Fragment fragment = (Fragment) object;
        //如果Item对应的Tag存在,则不进行刷新
        if (tags.indexOfValue(fragment.getTag()) > -1) {
            return super.getItemPosition(object);
        }
        return POSITION_NONE;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        //得到缓存的fragment
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        String tag = fragment.getTag();
        //保存每个Fragment的Tag
        tags.put(position, tag);
        return fragment;
    }

    //拿到指定位置的Fragment
    public Fragment getFragmentByPosition(int position) {
        return mFragmentManager.findFragmentByTag(tags.get(position));
    }

    public List getFragments(){
        return mFragmentManager.getFragments();
    }

    //刷新指定位置的Fragment
    public void notifyFragmentByPosition(int position) {
        tags.removeAt(position);
        notifyDataSetChanged();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        super.destroyItem(container, position, object);
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }
}
Fragmenr和Activity使用代码如下
   mFragments.add(mTab_01);
            mFragments.add(mTab_02);
            mFragments.add(mTab_03);
            mAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), mFragments);
            mViewPager.setOffscreenPageLimit(mFragments.size());
            //设置滑动监听器
            mViewPager.clearOnPageChangeListeners();
            mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                }

                //滑动时 改变图标状态
                @Override
                public void onPageSelected(int position) {
                    int currentItem = mViewPager.getCurrentItem();
                    LogUtils.d("当前currentItem:" + currentItem + "当前position:" + position);
                 
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });

        ......

        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(0);
        setSelect(0);

你可能感兴趣的:(Android 中 FragmentPagerAdapter 与 FragmentStatePagerAdapter的区别)