13

I got a ViewPager which holds Fragments via FragmentStatePagerAdapter. Let's say the pager initially holds the following pages (Fragments):

A - B - C - D

When the user swipes, he can move from A to B, B to C etc. But there are cases when the user changes some options on the A page, he can move not to B, but C:

A - C - D

Then the user goes back to A, modifies something and that re-enables B:

A - B - C - D

How can i achieve this very dynamic behavior? I cannot add Fragments any time when the user changes something and then re-populate the ViewPager, because it's slow and breaks the flow.

WonderCsabo
  • 11,947
  • 13
  • 63
  • 105
  • 1
    Similar to a `ListView`, you can simply update the `FragmentStatePagerAdapter` behind your `ViewPager` with different data. Give the `Adapter` a different set of `Fragments` in each case. – DroidBender Nov 08 '13 at 14:34
  • But if i call `notifyDatasetChanged`, the `ViewPager` recreates the whole list of `Fragment`s, and the jumps to the first page, also every `Fragment` loses the state. – WonderCsabo Nov 08 '13 at 15:09
  • 1
    Don't create your fragments in the ViewPager itself so they don't lose their state. Create them once, for example when the FragmentActivity is created. Afterwards, use the reference and don't create new ones. – DroidBender Nov 08 '13 at 15:13
  • That's not a bad idea. And what are you suggesting to avoid the ViewPager to jump to the first page when notified? – WonderCsabo Nov 08 '13 at 15:17
  • ViewPager.setCurrentItem(int position, boolean smoothscroll); – DroidBender Nov 08 '13 at 15:22
  • I try it, but it will flicker a bit, or not? I mean a first page flashes and that the current. – WonderCsabo Nov 08 '13 at 15:25

1 Answers1

3

We too had the same situation, and we solved this issue by maintaining a boolean ArrayList.The pages here are fragments. On the Fragment, we had written an interface to update the ArrayList.This interface is implemented on the parent activity.On every swipe of the ViewPager we are retrieving the boolean ArrayList. On ViewPager, in setOnPageChangeListener we overrided the onPageSelected.

And here is the piece of code:

pageIndicator.setOnPageChangeListener(new OnPageChangeListener() {

    @Override
    public void onPageSelected(int position) {
        // TODO Auto-generated method stub

        pagesPageBreakInfoList = activityContext.getBooleanList();
        currentPageNumber = position;

        if (!pagesPageBreakInfoList.get(position)) {
            if (currentPageNumber > previouspagenumber) {
                previouspagenumber = currentPageNumber;
                if (numberofSubmodule == (position + 1)) {
                    viewPager.setCurrentItem(position - 1);
                } else {
                    viewPager.setCurrentItem(position + 1);
                }
            } else {
                previouspagenumber = currentPageNumber;
                viewPager.setCurrentItem(position - 1);
            }
        } else {
            previouspagenumber = currentPageNumber;
        }
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub
    }
});
iamkaan
  • 1,495
  • 2
  • 23
  • 43
vamshi palutla
  • 104
  • 1
  • 6
  • Although user can still see the hidden fragment while scrolling, I guess this is the best I can find. – Sira Lam Oct 31 '17 at 08:18