Search code examples
androidandroid-fragmentsandroid-viewpagerfragmentpageradapterfragmentstatepageradapter

Make ViewPager forget current item position upon data change


I have a FragmentStatePagerAdapter that holds, say, three Fragments. The Fragments are A (position 0) , B, and C (position 2). Fragment B is currently visible.

Upon user interaction, I will call notifyDataSetChanged(), and there are now five Fragments. They are D (position 0), E, F, G and H (position 4).

The current behaviour is that Fragment E is shown by default after the data changes. (The ViewPager stays at its previous position.) However, I would like the ViewPager to show the position-0 item each time FragmentStatePagerAdapter gets some new data.

I have tried setCurrentItem(0) on the ViewPager. But Fragment E is still shown before the ViewPager scrolls to Fragment D.

Any solution?


Solution

  • You should use setCurrentItem(0, false) instead

    Edit:

    public void setCurrentItem (int item, boolean smoothScroll)

    false to disable smoothScroll

    http://developer.android.com/reference/android/support/v4/view/ViewPager.html#setCurrentItem%28int,%20boolean%29