Replace Fragment inside a ViewPager

后端 未结 18 1724
别那么骄傲
别那么骄傲 2020-11-22 00:26

I\'m trying to use Fragment with a ViewPager using the FragmentPagerAdapter. What I\'m looking for to achieve is to replace a fragment, positioned

18条回答
  •  庸人自扰
    2020-11-22 01:11

    I followed the answers by @wize and @mdelolmo and I got the solution. Thanks Tons. But, I tuned these solutions a little bit to improve the memory consumption.

    Problems I observed:

    They save the instance of Fragment which is replaced. In my case, it is a Fragment which holds MapView and I thought its costly. So, I am maintaining the FragmentPagerPositionChanged (POSITION_NONE or POSITION_UNCHANGED) instead of Fragment itself.

    Here is my implementation.

      public static class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
    
        private SwitchFragListener mSwitchFragListener;
        private Switch mToggle;
        private int pagerAdapterPosChanged = POSITION_UNCHANGED;
        private static final int TOGGLE_ENABLE_POS = 2;
    
    
        public DemoCollectionPagerAdapter(FragmentManager fm, Switch toggle) {
            super(fm);
            mToggle = toggle;
    
            mSwitchFragListener = new SwitchFragListener();
            mToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mSwitchFragListener.onSwitchToNextFragment();
                }
            });
        }
    
        @Override
        public Fragment getItem(int i) {
            switch (i)
            {
                case TOGGLE_ENABLE_POS:
                    if(mToggle.isChecked())
                    {
                        return TabReplaceFragment.getInstance();
                    }else
                    {
                        return DemoTab2Fragment.getInstance(i);
                    }
    
                default:
                    return DemoTabFragment.getInstance(i);
            }
        }
    
        @Override
        public int getCount() {
            return 5;
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
            return "Tab " + (position + 1);
        }
    
        @Override
        public int getItemPosition(Object object) {
    
            //  This check make sures getItem() is called only for the required Fragment
            if (object instanceof TabReplaceFragment
                    ||  object instanceof DemoTab2Fragment)
                return pagerAdapterPosChanged;
    
            return POSITION_UNCHANGED;
        }
    
        /**
         * Switch fragments Interface implementation
         */
        private final class SwitchFragListener implements
                SwitchFragInterface {
    
            SwitchFragListener() {}
    
            public void onSwitchToNextFragment() {
    
                pagerAdapterPosChanged = POSITION_NONE;
                notifyDataSetChanged();
            }
        }
    
        /**
         * Interface to switch frags
         */
        private interface SwitchFragInterface{
            void onSwitchToNextFragment();
        }
    }
    

    Demo link here.. https://youtu.be/l_62uhKkLyM

    For demo purpose, used 2 fragments TabReplaceFragment and DemoTab2Fragment at position two. In all the other cases I'm using DemoTabFragment instances.

    Explanation:

    I'm passing Switch from Activity to the DemoCollectionPagerAdapter. Based on the state of this switch we will display correct fragment. When the switch check is changed, I'm calling the SwitchFragListener's onSwitchToNextFragment method, where I'm changing the value of pagerAdapterPosChanged variable to POSITION_NONE. Check out more about POSITION_NONE. This will invalidate the getItem and I have logics to instantiate the right fragment over there. Sorry, if the explanation is a bit messy.

    Once again big thanks to @wize and @mdelolmo for the original idea.

    Hope this is helpful. :)

    Let me know if this implementation has any flaws. That will be greatly helpful for my project.

提交回复
热议问题