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?
Firstly set viewpager adapter to null then recreate the adapter and set i to it to viewpager. Show activity on this post. Define which message which is needed to update.
ViewPager in Android allows the user to flip left and right through pages of data. In our android ViewPager application we'll implement a ViewPager that swipes through three views with different images and texts.
This function is deprecated.
Exactly like my APP doing...
For change ViewPagerAdapter, what I did is :
Clear all Fragments inside current adapter
Set current adapter to null
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 MyPagerAdapter(getSupportFragmentManager());
Constants.VP.setAdapter(Constants.PAGER);
Hope it helps~
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With