I'm using a ViewPager from ViewPageIndicator and I need to be able to dynamically insert one fragment in the middle of others.
I've tried to manage the model with FragmentPagerAdapter and FragmentStatePagerAdapter (both from v4 support code) and the first seems to don't manage in any way insertion of pages in the middle. And the second only works if I make a naive implementation of getItemPosition returning always POSITION_NONE but this cause completly recreation of pages every time I swipe.
The problem I've observed with FragmentStatePagerAdapter (FSP) is this:
After looking a little in the code it seems that provided fragmentStatePagerAdapter doesn't support insertion in the middle. Is that correct or I missing something?
Update: The insert in the adapter is made in a logical way, when a certain codition is true the pages are incremented by one. The fragment creation is done using constructor in getItem() in that way:
void setCondition(boolean condition) {
this.condition=condition;
notifyDataSetChanged();
}
public int getCount(){
return condition?3:2;
}
public Fragment getItem(int position) {
if(position==0)
return new A();
else if(position==1)
return condition?new C():new B();
else if(position==2 && condition)
return new B();
else throw new RuntimeException("Not expected");
}
public int getItemPosition(Object object) {
if(object instanceof A) return 0;
else if(object instanceof B) return condition?2:1;
else if(object instanceof C) return 1;
}
Solution:
As said in accepted answer the key is to implement getItemId()
.
Make sure you use at least the R9(June 2012) version of android-support library. Because this method was added in it. Previous to this version that method doesn't exist and adapter doesn't manage correctly insertions. Also make sure you use FragmentPageAdapter because FragmentStatePagerAdapter still doesn't works because it doesn't use ids.
FragmentPagerAdapter is used exactly like FragmentStatePagerAdapter . It only differs in how it unloads your fragments when they are no longer needed (Figure 11.4). With FragmentStatePagerAdapter , your unneeded fragment is destroyed.
ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .
You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .
You forgot about one method.
Override getItemId(int position) from FragmentPagerAdapter
which simply returns position to return something what will identify fragment instance.
public long getItemId(int position) {
switch (position) {
case 0:
return 0xA;
case 1:
return condition ? 0xC : 0xB;
case 2:
if (condition) {
return 0xB;
}
default:
throw new IllegalStateException("Position out of bounds");
}
}
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