Not sure I have completely understood but FragmentStatePagerAdapter
load fragments around (forward and backward from...) the starting position. This behaviour is controlled by the adapter setOffscreenPageLimit(int number)
. The Javadoc states that the default offset is 1, meaning that when clicking "Frag B" following debug activity will note "Frag C" loading...owing to this offset (Frag B was loaded when Frag A/Frag C was loaded).
FragmentPageAdapter not working properly
-
05-10-2022 - |
题
I am using FragmentPagerAdapter to navigate between 4 Fragments in my app. But I am facing a strange problem. When I debugged my App I found that the Index value in the getItem loads to 0 when MainActivity is started and so the Fargment at 0th index is loaded and thereafter the index changes to 1 and the fragment at 1st index is loaded and because of this the sequence of getting the fragment is a little bit disturbed. I dont know if I am missing out at something. But I really cant figure out the problem. Here is my FragmentPagerAdapter Class:
public class TabsPagerAdapter extends FragmentStatePagerAdapter {
Context context;
public TabsPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new FragmentA();
case 1:
return new FragmentB();
case 2:
return new FragmentC();
case 3:
return new FragmentD();
}
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 4;
}
}
This class is not behaving the way i Want it to work. The UI gets loaded according to the way I want. I mean , Suppose if i click on the Tab of FragmentB(), The index would be generated of the next Fragment i.e. FragmentC() but on my device, I will see the FragmentB() I know I am unable to explain the issue properly but still i would be grateful if anyone can help but please dont downvote. Thanks in advance :)
解决方案