美文网首页
Viewpager中的Fragment 滑动时保存状态的问题

Viewpager中的Fragment 滑动时保存状态的问题

作者: 阿狸_小乖 | 来源:发表于2017-07-15 18:07 被阅读0次
    package com.ynzlkj.selfproject.UserMode.adapter;
    
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.view.ViewGroup;
    
    import java.util.List;
    
    /**
     * Created by 陈骏 on 2017/7/14 0014.
     * QQ:200622550
     * 作用:首页Tab适配
     */
    
    public class TabFragmentPagerAdapter extends FragmentPagerAdapter {
    
        public FragmentManager fm;
        private List<Fragment> mFragments;
        private List<String> mTitles;
    
        public TabFragmentPagerAdapter(FragmentManager fragmentManager, List<Fragment> fragments) {
            super(fragmentManager);
            fm = fragmentManager;
            mFragments = fragments;
        }
    
        public TabFragmentPagerAdapter(FragmentManager fragmentManager, List<Fragment> fragments, List<String> titles) {
            super(fragmentManager);
            fm = fragmentManager;
            mFragments = fragments;
            mTitles = titles;
        }
    
    //    @Override
    //    public Fragment getItem(int position) {
    //        return mFragments.get(position);
    //    }
        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            fragment = mFragments.get(position);
            Bundle bundle = new Bundle();
            bundle.putString("id", "" + position);
            fragment.setArguments(bundle);
            return fragment;
        }
    
        @Override
        public int getCount() {
            return mFragments.size();
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles != null ? mTitles.get(position) : "";
        }
    
        @Override
        public Fragment instantiateItem(ViewGroup container, int position) {
            Fragment fragment = (Fragment) super.instantiateItem(container,
                    position);
            fm.beginTransaction().show(fragment).commit();
            return fragment;
        }
    
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // super.destroyItem(container, position, object);
            Fragment fragment = mFragments.get(position);
            fm.beginTransaction().hide(fragment).commit();
        }
    }
    

    相关文章

      网友评论

          本文标题:Viewpager中的Fragment 滑动时保存状态的问题

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