Replace fragment position inside ViewPager

I have a ViewPager that contains two fragments [ Fragment 1] [Fragment 2] . Now, based on the selection in [Fragment 1] , a new To ViewPager fragment will be added and it will look like this

[Fragment 1] [NewAddedFragment] [Fragment 2]

and also based on user selection in the first fragment, I can remove NewAddedFragment from ViewPager . Then it will look like

[ Fragment 1] [Fragment 2]

+4
source share
1 answer

Look at your โ€œAdapterโ€, which implements the 'FragmentPagerAdapter', and something will contain a collection type field, such as an ArrayList that supports the adapter.

Depending on the implementation of dataStructure, you can change the base collection by calling the appropriate add, install, and paste methods in the fragmentpageradapter.

Then your next call to get a new page in ViewPager should reflect a change in the base collection. If this is not the case, you may have to call "commit" in the FragmentManager, but I don't think it is necessary in this case.

public class BookPagerAdapter extends FragmentPagerAdapter {private int currentPage = 0; private ArrayList mFragments;

 public BookPagerAdapter(FragmentManager fm) { super(fm); mFragments = new ArrayList<Fragment>(); mFragments.add(new PageFragment(BitmapFactory.decodeResource(getResources(), R.drawable.icon_construction32), "TitlePage")); } 

collection change example:

 bpa.mFragments.add(new PageFragment(map)); 
-1
source

All Articles