android viewpager change adapter

android-viewpager change page from fragment
android-viewpager re-set adapter
how to clear viewpager adapter in android
dynamic viewpager (android)
how to refresh viewpager adapter in android
android-viewpager with one fragment
android refresh fragment pager adapter
viewpager with fragments example in android

I'm developing an app with a ViewPager for lateral scrolling tables, sometimes i need to change the adapter to load a different set of tables. I've tried to do the following:

mViewPager.setAdapter(new pagerAdapterPushed(getSupportFragmentManager()));
mViewPager.getAdapter().notifyDataSetChanged();
indicator.setViewPager(mViewPager);
indicator.invalidate();
mViewPager.invalidate();

but it always reload the previous data. Isn't it possibile to change the adapter of a ViewPager?

Exactly like my APP doing...

For change ViewPagerAdapter, what I did is :

  1. Clear all Fragments inside current adapter
  2. Set current adapter to null
  3. Assign new adpater for ViewPager

    /* Clear all Fragments inside current adapter */
    public class MyPagerAdapter extends FragmentPagerAdapter
    {
      private ArrayList<Fragment> fragments=new ArrayList<Fragment>();
      //...some stuff
    
      public void clearAll() //Clear all page
      {
        for(int i=0; i<fragments.size(); i++)
        fragMan.beginTransaction().remove(fragments.get(i)).commit();
        fragments.clear();
      }
    }
    
    /* Set current adapter to null */
    Constants.VP.removeAllViews();
    Constants.VP.setAdapter(null);
    
    /* Assign new adpater for ViewPager */
    Constants.PAGER=new MyewPagerAdapter(getSupportFragmentManager());
    Constants.VP.setAdapter(Constants.PAGER);
    

Hope it helps~

Updating ViewPager With New Data Dynamically, It is used in conjunction with PagerAdapter, FragmentPagerAdapter or code of a sample application from here to run the android application. If new data is not equal to old data, we replace the old data with new one using  ViewPager.OnAdapterChangeListener This package is part of the Android support library which is no longer maintained. The support library has been superseded by AndroidX which is part of Jetpack .

You need to override public int getItemPosition (Object object) in pager adapter. For every element in the viewpager, you need to return POSITION_UNCHANGED (if it is still in the pager) or POSITION_NONE (if it was removed). Otherwise the view pager won't know something has changed. I wouldn't recommend setting a new adapter, just clearing out the data in the old one.

Dynamic ViewPager in android - Explore yourself, ViewPager is one of the most commonly used component in android. It is a good way to create our view pager by extending PagerAdapter. Here notifyDataSetChanged() is useful only when there is change in view pager data (​either items  Android Platform; Android Support Library Called when the adapter for the given view pager has changed. ViewPager where the adapter change has happened

I see that @RRTW way worked for some people but it did not do the trick for me (although it was close). Without 'commitNow()' instead of regular 'commit()' it would not work properly. I did something like that:

    private fun changePagerAdapter(newAdapter: PagerAdapter) {
    val transaction = supportFragmentManager.beginTransaction()
    supportFragmentManager.fragments.forEach {
        transaction.remove(it)
    }
    transaction.commitNow()
    view_pager.adapter = newAdapter
}

ViewPager with FragmentPagerAdapter, Finally, let's associate the ViewPager with a new instance of our adapter: public class MainActivity Attach the page change listener inside the activity vpPager. public static interface ViewPager.OnAdapterChangeListener androidx.viewpager.widget.ViewPager.OnAdapterChangeListener Callback interface for responding to adapter changes.

I found a very simple option, I do not know if it's right or wrong, but you can just re-set the adapter

mViewPager.setAdapter(mSectionsPagerAdapter);

It's example for google example project Tabbed Activity for button FloatingActionButton

Migrate from ViewPager to ViewPager2, Unregister an observer from callbacks related to the adapter's data changing. Inherited methods. From class java.lang.Object  ViewPager associates each page with a key Object instead of working with Views directly. This key is used to track and uniquely identify a given page independent of its position in the adapter. A call to the PagerAdapter method startUpdate(ViewGroup) indicates that the contents of the ViewPager are about to change.

Add this to view pager to reload for next fragment:

viewPager.setCurrentItem()

PagerAdapter, When using FragmentPagerAdapter the host ViewPager must have a an observer to receive callbacks related to the adapter's data changing. Called when the scroll state changes. This method will be invoked when the current page is scrolled, either as part of a programmatically initiated smooth scroll or a user initiated touch scroll. This method will be invoked when a new page becomes selected.

FragmentStatePagerAdapter, Called when a change in the shown pages is going to start being made. Inherited methods. From class androidx.viewpager.widget.PagerAdapter  In certain situations your app might even want to have a ViewPager that allows switching pages using an indicator but that doesn't intercept swipe events at all. This is usually because we want to have the swipe events perform another action rather than change the page. The first step is to define a custom ViewPager

FragmentPagerAdapter, Callback interface for responding to changing state of the selected page. Summary. Public methods. abstract void, onPageScrollStateChanged(  In certain situations your app might even want to have a ViewPager that allows switching pages using an indicator but that doesn't intercept swipe events at all. This is usually because we want to have the swipe events perform another action rather than change the page. The first step is to define a custom ViewPager subclass called

ViewPager.OnPageChangeListener, A data set change may involve pages being added, removed, or changing position. The ViewPager will keep the current page active provided the adapter  ViewPager is a layout manager that allows users to flip and view pages left and right. It is used in conjunction with PagerAdapter, FragmentPagerAdapter or FragmentStatePagerAdapter. We attach adapter consisting of either Fragment objects or simple View objects.

Comments
  • I tried your solution. It work perfect without any issues. thanks a lot
  • In API 18 I couldn't setAdapter(null). However everything else worked. Thanks.
  • Thank you so much! I have been looking for this for some days now!
  • For me, the only necessary part was removing the fragments using fragment manager transactions. The rest was unnecessary (i.e. removeAllViews, setAdapter(null), etc).
  • For some reason this only worked for me after I changed it from FragmentPagerAdapter to FragmentStatePagerAdapter
  • This does not work after the adapter is set I am afraid.