I\'m using a ViewPager together with a FragmentPagerAdapter to host three different fragments
[Fragment1][Fragment2][Fragment3]
What I\'m t
Shouldn't your getItem
statement be returning fragment4 for case 0
in some circumstances?
Like Maurycy said, you need to call mAdapter.notifyDataSetChanged()
whenever you want to tell the ViewPager that the fragments have been replaced. However, you also need to override the getItemPosition()
abstract function in your adapter and return POSITION_NONE when called with an old, to be hidden, fragment as argument. Otherwise, ViewPager will retain all current fragments.
For more information and example code, see this answer to a related question.
Try setting offScreenPageLimit to 5 (or based on your usecase) to avoid reloading of the fragment so that what you had replaced will stay instead of getting reloaded when you swipe.
mViewPager.setOffscreenPageLimit(5);
Try using FragmentStatePagerAdapter
http://android-developers.blogspot.com/2011_08_01_archive.html
coz it will remove the fragment as you swipe through the view(also when you call getitem), so you don't need to explicitly remove them yourself.
I've been having the same problem it works for me.
I try your code and remove the if (fragment!=null)
block and it works.
Hey I have solved this problem by using a List inside my FragmentPagerAdapter to handle the various fragments and then my getItem is like so....
public void addScreen(String tag, Class<?> clss, Bundle args){
if(clss!=null){
TabInfo info = new TabInfo(tag, clss, args);
screens.add(info);
}
}
@Override
public Fragment getItem(int position) {
TabInfo info = screens.get(position);
return Fragment.instantiate(context, info.clss.getName(), info.args);
}
I then programmatically remove/add/rearrange the list items (fragments) then call on the handler
mAdapter.notifyDataSetChanged()