美文网首页
Fragment嵌套Fragment之后,滑动超过2个Fragm

Fragment嵌套Fragment之后,滑动超过2个Fragm

作者: 指尖流逝的青春 | 来源:发表于2017-12-11 15:50 被阅读0次
    public class FragmentAdapter extends FragmentPagerAdapter {
    
        private FragmentManager fragmentManager;
        private List<Fragment> fragmentList;
    
        public FragmentAdapter(FragmentManager fm, FragmentManager fragmentManager) {
            super(fm);
            this.fragmentManager = fragmentManager;
        }
    
        public FragmentAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            this.fragmentManager = fm;
            this.fragmentList = fragmentList;
        }
    
        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            fragment = fragmentList.get(position);
            Bundle bundle = new Bundle();
            bundle.putString("id", position + "");
            fragment.setArguments(bundle);
            return fragment;
        }
    
        @Override
        public int getCount() {
            return fragmentList.size();
        }
    
        @NonNull
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Fragment fragment = (Fragment) super.instantiateItem(container, position);
            fragmentManager.beginTransaction().show(fragment).commit();
            return fragment;
        }
    
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            Fragment fragment = fragmentList.get(position);
            fragmentManager.beginTransaction().hide(fragment).commit();
        }
    }
    

    相关文章

      网友评论

          本文标题:Fragment嵌套Fragment之后,滑动超过2个Fragm

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