Replace Fragment inside a ViewPager

后端 未结 18 1338
别那么骄傲
别那么骄傲 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 01:01

    Works Great with AndroidTeam's solution, however I found that I needed the ability to go back much like FrgmentTransaction.addToBackStack(null) But merely adding this will only cause the Fragment to be replaced without notifying the ViewPager. Combining the provided solution with this minor enhancement will allow you to return to the previous state by merely overriding the activity's onBackPressed() method. The biggest drawback is that it will only go back one at a time which may result in multiple back clicks

    private ArrayList bFragments = new ArrayList();
    private ArrayList bPosition = new ArrayList();
    
    public void replaceFragmentsWithBackOut(ViewPager container, Fragment oldFragment, Fragment newFragment) {
        startUpdate(container);
    
        // remove old fragment
    
        if (mCurTransaction == null) {
             mCurTransaction = mFragmentManager.beginTransaction();
         }
        int position = getFragmentPosition(oldFragment);
         while (mSavedState.size() <= position) {
             mSavedState.add(null);
         }
    
         //Add Fragment to Back List
         bFragments.add(oldFragment);
    
         //Add Pager Position to Back List
         bPosition.add(position);
    
         mSavedState.set(position, null);
         mFragments.set(position, null);
    
         mCurTransaction.remove(oldFragment);
    
         // add new fragment
    
         while (mFragments.size() <= position) {
             mFragments.add(null);
         }
         mFragments.set(position, newFragment);
         mCurTransaction.add(container.getId(), newFragment);
    
        finishUpdate(container);
    
        // ensure getItem returns newFragemtn after calling handleGetItemInbalidated()
        handleGetItemInvalidated(container, oldFragment, newFragment);
    
        container.notifyItemChanged(oldFragment, newFragment);
     }
    
    
    public boolean popBackImmediate(ViewPager container){
        int bFragSize = bFragments.size();
        int bPosSize = bPosition.size();
    
        if(bFragSize>0 && bPosSize>0){
            if(bFragSize==bPosSize){
                int last = bFragSize-1;
                int position = bPosition.get(last);
    
                //Returns Fragment Currently at this position
                Fragment replacedFragment = mFragments.get(position);               
                Fragment originalFragment = bFragments.get(last);
    
                this.replaceFragments(container, replacedFragment, originalFragment);
    
                bPosition.remove(last);
                bFragments.remove(last);
    
                return true;
            }
        }
    
        return false;       
    }
    

    Hope this helps someone.

    Also as far as getFragmentPosition() goes it's pretty much getItem() in reverse. You know which fragments go where, just make sure you return the correct position it will be in. Here's an example:

        @Override
        protected int getFragmentPosition(Fragment fragment) {
                if(fragment.equals(originalFragment1)){
                    return 0;
                }
                if(fragment.equals(replacementFragment1)){
                    return 0;
                }
                if(fragment.equals(Fragment2)){
                    return 1;
                }
            return -1;
        }
    

提交回复
热议问题