Question

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.

Was it helpful?

Solution

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");
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top