Replace Fragment inside a ViewPager

后端 未结 18 1370
别那么骄傲
别那么骄傲 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:10

    Based on @wize 's answer, which I found helpful and elegant, I could achieve what I wanted partially, cause I wanted the cability to go back to the first Fragment once replaced. I achieved it bit modifying a bit his code.

    This would be the FragmentPagerAdapter:

    public static class MyAdapter extends FragmentPagerAdapter {
        private final class CalendarPageListener implements
                CalendarPageFragmentListener {
            public void onSwitchToNextFragment() {
                mFragmentManager.beginTransaction().remove(mFragmentAtPos0)
                        .commit();
                if (mFragmentAtPos0 instanceof FirstFragment){
                    mFragmentAtPos0 = NextFragment.newInstance(listener);
                }else{ // Instance of NextFragment
                    mFragmentAtPos0 = FirstFragment.newInstance(listener);
                }
                notifyDataSetChanged();
            }
        }
    
        CalendarPageListener listener = new CalendarPageListener();;
        private Fragment mFragmentAtPos0;
        private FragmentManager mFragmentManager;
    
        public MyAdapter(FragmentManager fm) {
            super(fm);
            mFragmentManager = fm;
        }
    
        @Override
        public int getCount() {
            return NUM_ITEMS;
        }
    
        @Override
        public int getItemPosition(Object object) {
            if (object instanceof FirstFragment && mFragmentAtPos0 instanceof NextFragment)
                return POSITION_NONE;
            if (object instanceof NextFragment && mFragmentAtPos0 instanceof FirstFragment)
                return POSITION_NONE;
            return POSITION_UNCHANGED;
        }
    
        @Override
        public Fragment getItem(int position) {
            if (position == 0)
                return Portada.newInstance();
            if (position == 1) { // Position where you want to replace fragments
                if (mFragmentAtPos0 == null) {
                    mFragmentAtPos0 = FirstFragment.newInstance(listener);
                }
                return mFragmentAtPos0;
            }
            if (position == 2)
                return Clasificacion.newInstance();
            if (position == 3)
                return Informacion.newInstance();
    
            return null;
        }
    }
    
    public interface CalendarPageFragmentListener {
        void onSwitchToNextFragment();
    }
    

    To perfom the replacement, simply define a static field, of the type CalendarPageFragmentListener and initialized through the newInstance methods of the corresponding fragments and call FirstFragment.pageListener.onSwitchToNextFragment() or NextFragment.pageListener.onSwitchToNextFragment() respictevely.

提交回复
热议问题