Replace Fragment inside a ViewPager

后端 未结 18 1369
别那么骄傲
别那么骄傲 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 doing something to similar to wize but in my answer yo can change between the two fragments whenever you want. And with the wize answer I have some problems when changing the orientation of the screen an things like that. This is the PagerAdapter looks like:

        public class MyAdapter extends FragmentPagerAdapter
    {
        static final int NUM_ITEMS = 2;
        private final FragmentManager mFragmentManager;
        private Fragment mFragmentAtPos0;
         private Map mFragmentTags;
         private boolean isNextFragment=false;
    
        public MyAdapter(FragmentManager fm)
        {
            super(fm);
            mFragmentManager = fm;
             mFragmentTags = new HashMap();
        }
    
        @Override
        public Fragment getItem(int position)
        {
            if (position == 0)
            {
    
    
                if (isPager) {
                    mFragmentAtPos0 = new FirstPageFragment();
                } else {
                    mFragmentAtPos0 = new NextFragment();
                }
                return mFragmentAtPos0;
            }
            else
                return SecondPageFragment.newInstance();
        }
    
        @Override
        public int getCount()
        {
            return NUM_ITEMS;
        }
    
    
     @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Object obj = super.instantiateItem(container, position);
            if (obj instanceof Fragment) {
                // record the fragment tag here.
                Fragment f = (Fragment) obj;
                String tag = f.getTag();
                mFragmentTags.put(position, tag);
            }
            return obj;
        }
    
    
        public void onChange(boolean isNextFragment) {
    
            if (mFragmentAtPos0 == null)
                mFragmentAtPos0 = getFragment(0);
            if (mFragmentAtPos0 != null)
                mFragmentManager.beginTransaction().remove(mFragmentAtPos0).commit();
    
    
            if (!isNextFragment) {
                mFragmentAtFlashcards = new FirstPageFragment();
            } else {
                mFragmentAtFlashcards = new NextFragment();
            }
    
            notifyDataSetChanged();
    
    
        }
    
    
        @Override
        public int getItemPosition(Object object)
        {
            if (object instanceof FirstPageFragment && mFragmentAtPos0 instanceof NextFragment)
                return POSITION_NONE;
             if (object instanceof NextFragment && mFragmentAtPos0 instanceof FirstPageFragment)
                return POSITION_NONE;
            return POSITION_UNCHANGED;
        }
    
    
        public Fragment getFragment(int position) {
            String tag = mFragmentTags.get(position);
            if (tag == null)
                return null;
            return mFragmentManager.findFragmentByTag(tag);
        }
    }
    

    The listener I implemented in the adapter container activity to put it to the fragment when attaching it, this is the activity:

        public class PagerContainerActivity extends AppCompatActivity implements ChangeFragmentListener {
    
    //...
    
      @Override
        public void onChange(boolean isNextFragment) {
            if (pagerAdapter != null)
                pagerAdapter.onChange(isNextFragment);
    
    
        }
    
    //...
    }
    

    Then in the fragment putting the listener when attach an calling it:

    public class FirstPageFragment extends Fragment{
    
    
    private ChangeFragmentListener changeFragmentListener;
    
    
    //...
     @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            changeFragmentListener = ((PagerContainerActivity) activity);
        }
    
        @Override
        public void onDetach() {
            super.onDetach();
            changeFragmentListener = null;
        }
    //...
    //in the on click to change the fragment
    changeFragmentListener.onChange(true);
    //...
    }
    

    And finally the listener:

    public interface changeFragmentListener {
    
        void onChange(boolean isNextFragment);
    
    }
    

提交回复
热议问题