Insert pages in the middle of a FragmentPageAdapter
Asked Answered
K

1

15

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:

  • I start with two pages [A][B]
  • Then I insert [C] in the middle [A][C][B]. After the insert I call notifyDataSetchange()
  • Then FSP calls getItemPosition for [A] and gets 0
  • Then FSP calls geTItemPosition for [B] and gets 2. And it says... Ohhh I've to destroy [B] and makes mFragments.set(2, null) and then as it only have two elements in mFragments array it throws an IndexOutOfBoundsException

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.

Kugler answered 31/5, 2012 at 19:43 Comment(3)
What do you mean by "inserting"? How do you instantiate your Fragments in your PagerAdapter? Like this: androiddesignpatterns.com/2012/05/… ? Or maybe just post the code of your Adapter and the way you insert it.Dauphine
I create them using constructor. I've updated the question with more info.Kugler
Did you check if the ViewPager has three items if the condition is true on the creation of the Adapter (Activity)? If it does, than it's probably some problem with notifying. Interesting question...Dauphine
E
5

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");
    }
}
Ellenaellender answered 27/7, 2012 at 9:46 Comment(1)
I had some confusion on that. I was working with revision 8 of support library. And the method you mention has appeared in R9 (June 2012) developer.android.com/tools/extras/support-library.html. In new revision FragmentPagerAdapter makes use of it and it works correctly. FragmentStatePagerAdapter doesn't seems to use it and still doesn't work correctly.Kugler

© 2022 - 2024 — McMap. All rights reserved.