I have a FragmentPagerAdapter for a viewPager Which initially has only one Fragment in it. I want to dynamically add a new Fragment to the adapter when user swipes from right to left, and dynamically remove a Fragment when user swipes from left to right.I have tried to use this library https://github.com/commonsguy/cwac-pager but in that library we have an option to add and remove fragments on button clicks. I have tried to add a OnPageChangeListener to the viewpager but the callback methods ( onPageScrolled and onPageScrollStateChanged) are being called more than once which results in addition of more than one fragment to the FragmentPagerAdapter. So please shed some light on how to do this.
3 Answers
@dora: i think in your case FragmentStatePagerAdapter will help you. I have mentioned its use below as per my understanding.I hope it will help you in taking decision.
There are two ways to implement ViewPager: • FragmentStatePagerAdapter • FragmentPagerAdapter
FragmentStatePagerAdapter class consumes less memory, because it destroys fragments, as soon as they are not visible to user, keeping only saved state of that fragment
FragmentPagerAdapter: when there are less number of fragments. But using AndroidFragmentPagerAdapter for large number of fragments would result choppy and laggy UX.
Number of page hold by a viewPager? The number of items that any ViewPager will keep hold of is set by the setOffscreenPageLimit() method. The default value for the offscreen page limit is 3. This means ViewPager will track the currently visible page, one to the left, and one to the right. The number of tracked pages is always centered around the currently visible page.
Please follow this link for code: http://www.truiton.com/2013/05/android-fragmentpageradapter-example/

- 1,790
- 1
- 16
- 32
I know this post is old, but I struggled to figure this out so I'll answer it anyway.
You want to use FragmentStatePagerAdapter
and override getItemPosition()
. Create a list of stuff you want to pass down to the fragment, call notifyDataSetChanged()
, and you're all set!
Here's the adapter:
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
List<String> mKeyList = new ArrayList<>();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(mKeyList.get(position));
}
@Override
public int getCount() {
return mKeyList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return "SCOUT " + (getCount() - position);
}
public void add(int position, String key) {
mKeyList.add(position, key);
notifyDataSetChanged();
}
}
And here's the fragment:
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SCOUT_KEY = "scout_key";
public static PlaceholderFragment newInstance(String key) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putString(ARG_SCOUT_KEY, key);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.current_scout_fragment, container, false);
//getArguments().getString(ARG_SCOUT_KEY));
return rootView;
}
}

- 3,929
- 4
- 32
- 61
-
Ohh, God.. Thanks man, found this solution after 2days. working awesome. Thanks alot. Achieved by extending FragmentStatePagerAdapter instead of FragmentPagerAdapter – sandeepmaaram Nov 05 '16 at 13:50
-
Add is easy now where is case of remove? – Rajesh N Jun 27 '17 at 11:17
-
It does and I've since moved on. I now use a [custom class](https://github.com/SUPERCILEX/Robot-Scouter/blob/4395fca46956b7b7a047c45e7ed9b3f6dcb809ff/app/src/main/java/com/supercilex/robotscouter/util/ui/MovableFragmentStatePagerAdapter.kt) which supports moving items around. If you call `notifyDataSetChanged()`, it'll only add or remove fragments as needed. This does mean that each fragment has to manage updating itself though, as notify won't refresh the fragments. If you use something like Firebase or Room, you'll be fine. – SUPERCILEX Mar 01 '18 at 08:13
I want to dynamically add a new Fragment to the adapter when user swipes from right to left, and remove dynamically remove a Fragment when user swipes from left to right.
AFAIK, that will not be supported by any PagerAdadpter
. It certainly will not be supported by ArrayPagerAdapter
. The page needs to exist, otherwise you cannot swipe to it. You cannot swipe first, then add the page later.
Moreover, I have never found a use case for your proposed pattern that could not be handled by having the page be in the adapter, but not populating the page (i.e., whatever the expensive work is that you appear to be trying to avoid) until the swipe begins.

- 986,068
- 189
- 2,389
- 2,491
-
:i am trying to implement a calendar so whenever the user swipes from right to left,he goes to next week.but there is also a button on the top of view pager that enables user to switch months.so imagine this scenario,suppose the user first uses month switcher buton to go to next month(in switch month butonclick i write viewpager.setCurrentItem(curentposition+1)) and then later decides to swipe the viewpager to go to previous week.the user wont be able to return to the date where he started because there is only one frament inthe backstack.My Need:he shud go back to where he started – dora Nov 19 '13 at 13:37
-
@dora: "i am trying to implement a calendar so whenever the user swipes from right to left,he goes to next week" -- `ViewPager` is not well-suited for that role, as there are an infinite number of weeks in the future, and a whole lotta weeks in the past. – CommonsWare Nov 19 '13 at 15:27
-
i dont need to navigate to past.all i need to be able to navigate is current time and future.so please help me out if there is way for it – dora Nov 19 '13 at 16:52
-
@dora: Again I would not use `ViewPager`, but would roll something else using gestures. – CommonsWare Nov 19 '13 at 16:54