Getting the current Fragment instance in the viewpager

前端 未结 30 1928
醉话见心
醉话见心 2020-11-22 08:56

Below is my code which has 3 Fragment classes each embedded with each of the 3 tabs on ViewPager. I have a menu option. As shown in the onOpt

30条回答
  •  醉酒成梦
    2020-11-22 08:59

        public class MyPagerAdapter extends FragmentPagerAdapter {
            private Fragment mCurrentFragment;
    
            public Fragment getCurrentFragment() {
                return mCurrentFragment;
            }
    //...    
            @Override
            public void setPrimaryItem(ViewGroup container, int position, Object object) {
                if (getCurrentFragment() != object) {
                    mCurrentFragment = ((Fragment) object);
                }
                super.setPrimaryItem(container, position, object);
            }
        }
    

提交回复
热议问题