美文网首页
ViewPager适配器编写

ViewPager适配器编写

作者: 强0808 | 来源:发表于2018-03-18 11:02 被阅读0次

    ViewPager的适配器的编写

      public class FixPagerAdapter extends FragmentStatePagerAdapter {
    
    //顶部TabLayout的文字
    private String[] titles;
    //保存Fragment的集合
    private List<Fragment> fragments = null;
    
    public void setFragments(List<Fragment> fragments) {
        this.fragments = fragments;
    
    }
    
    public void setTitles(String[] titles){
        this.titles=titles;
    
    }
    
    public FixPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    
    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }
    
    @Override
    public int getCount() {
        return fragments.size();
    }
    
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = null;
        try {
            fragment = (Fragment) super.instantiateItem(container, position);
    
        } catch (Exception e) {
    
        }
    
        return fragment;
    }
    
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    
    }
    
    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
    

    }

    ViewPager继承FragmentPagerAdapter和FragmentStatePagerAdapter两种的区别

     两种适配器的写法是一样的,显示出来的效果基本是一样的,
     FragmentPagerAdapter适用于当viewpager中fragment数量少的
    情况,FragmentStatePagerAdapter适用于fragment数量多的情况
    下使用
    两种adapter的创建,储存方式不同
    两种adapter的销毁方式不同
    

    FragmentPagerAdapter

    创建
    @SuppressWarnings("ReferenceEquality")
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        if (mCurTransaction == null) {
            mCurTransaction = mFragmentManager.beginTransaction();
        }
    
        final long itemId = getItemId(position);
    
        // Do we already have this fragment?
        String name = makeFragmentName(container.getId(), itemId);
        Fragment fragment = mFragmentManager.findFragmentByTag(name);
        if (fragment != null) {
            if (DEBUG) Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment);
            mCurTransaction.attach(fragment);
        } else {
            fragment = getItem(position);
            if (DEBUG) Log.v(TAG, "Adding item #" + itemId + ": f=" + fragment);
            mCurTransaction.add(container.getId(), fragment,
                    makeFragmentName(container.getId(), itemId));
        }
        if (fragment != mCurrentPrimaryItem) {
            fragment.setMenuVisibility(false);
            fragment.setUserVisibleHint(false);
        }
    
        return fragment;
    }
    
    销毁
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (mCurTransaction == null) {
            mCurTransaction = mFragmentManager.beginTransaction();
        }
        if (DEBUG) Log.v(TAG, "Detaching item #" + getItemId(position) + ": f=" + object
                + " v=" + ((Fragment)object).getView());
        mCurTransaction.detach((Fragment)object);
    }
    创建时,以键值对的形式,存储id,fragment,fragment名字
    销毁时,不是将不可见的fragment销毁,而是
    仅仅将该fragment从页面中detach掉,fragment还是在manager
    中保存,内存没有被释放,所以FragmentPagerAdapter不适合
    fragment数量多的情况下使用,因
    为未被释放的fragment会占用大量内存。
    

    FragmentStatePagerAdapter

    创建
     @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // If we already have this item instantiated, there is nothing
        // to do.  This can happen when we are restoring the entire pager
        // from its saved state, where the fragment manager has already
        // taken care of restoring the fragments we previously had instantiated.
        if (mFragments.size() > position) {
            Fragment f = mFragments.get(position);
            if (f != null) {
                return f;
            }
        }
    
        if (mCurTransaction == null) {
            mCurTransaction = mFragmentManager.beginTransaction();
        }
    
        Fragment fragment = getItem(position);
        if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
        if (mSavedState.size() > position) {
            Fragment.SavedState fss = mSavedState.get(position);
            if (fss != null) {
                fragment.setInitialSavedState(fss);
            }
        }
        while (mFragments.size() <= position) {
            mFragments.add(null);
        }
        fragment.setMenuVisibility(false);
        fragment.setUserVisibleHint(false);
        mFragments.set(position, fragment);
        mCurTransaction.add(container.getId(), fragment);
    
        return fragment;
    }
    
    销毁
      @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        Fragment fragment = (Fragment) object;
    
        if (mCurTransaction == null) {
            mCurTransaction = mFragmentManager.beginTransaction();
        }
        if (DEBUG) Log.v(TAG, "Removing item #" + position + ": f=" + object
                + " v=" + ((Fragment)object).getView());
        while (mSavedState.size() <= position) {
            mSavedState.add(null);
        }
        mSavedState.set(position, fragment.isAdded()
                ? mFragmentManager.saveFragmentInstanceState(fragment) : null);
        mFragments.set(position, null);
    
        mCurTransaction.remove(fragment);
    }
    
    创建时会保存fragment位置状态,fragment,
    销毁时会保存fragment位置状态,这样就会保存当前fragment的   
    位置状态,创建时候,在当前位置创建,节约资源

    相关文章

      网友评论

          本文标题:ViewPager适配器编写

          本文链接:https://www.haomeiwen.com/subject/qhqeqftx.html