I have a viewpager with 5 tabs and if I go to the 5th fragment(that is the fragment loaded when I click the 5th tab or swipe to the 5th tab) and then I go to the 3rd tab the 5th fragment won't load the view anymore and if I go to the 2nd or 1st tab the 4th and 5th tabs won't. I tried changing the viewpager.offscreenlimit(3) or even 5 with no results. If it helps anyone figure it out, it happens regardless of what fragments I put in place of the 3rd 4th and 5th positions. Also, the first viewpager change takes a while , about 1.5 seconds.
here is my code
mport android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;
import java.util.List;
public class AppSectionsPagerAdapter extends FragmentStatePagerAdapter
{
private static String[] fragmentNames = { "Example0", "Example1", "Example2","Example3","Example4"};
private FragmentManager fragmentManager = null;
private List<Fragment> fragments=null;
Fragment example0;
Fragment example1;
Fragment example2;
Fragment example3;
Fragment example4;
int numFragments;
int errorcode;
public AppSectionsPagerAdapter(FragmentManager fm)
{
super(fm);
numFragments = 5;
this.fragmentManager = fm;
example0 = new ListFragment();
example1 = new ListFragment();
example2 = new ListFragment();
example3 = new Fragment();
example4 = new Fragment();
}
@Override
public Fragment getItem(int i)
{
switch (i)
{
case 0:
return example0;
case 1:
return example1;
case 2:
return example2;
case 3:
return example3;
case 4:
return example4;
default:
Fragment frag = new Error_Fragment();
Bundle args = new Bundle();
errorcode = -5;
//args.putInt(errorcode, i + 1);
frag.setArguments(args);
return frag;
}
}
@Override
public int getCount()
{
return numFragments;
}
@Override
public CharSequence getPageTitle(int position)
{
return fragmentNames[position];
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object)
{
super.setPrimaryItem(container,0,object);
}
@Override
public void notifyDataSetChanged()
{
super.notifyDataSetChanged();
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view)
{
fragmentManager.executePendingTransactions();
//fragmentManager.saveFragmentInstanceState(fragments.get(position));
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…