I asked this question over a year ago and got some good feedback about how to implement it but that project was abandoned. It has now resurfaced and I'm rekindling this fire.
The closest answer I got is here: Android ViewPager: Move any page to end programatically?
Essentially what I'm trying to do is reorder view fragments (and add new view fragments) based on data that I retrieve from a web service. The solution I posted the link to above works with a FragmentPagerAdapter but does not appear to work with a FragmentStatePagerAdapter. When I call notifyDataSetChanged() after rebuilding my data set, when using FragmentStatePagerAdapter, I get outofbounds exceptions because the size of my Array is not large enough. If I change everything up to use FragmentPagerAdapter, I do not get that exception, but I also am not seeing the fragments ordered correctly.
here is my fragmentstatepageradapter:
public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
public int getItemId(int position) {
//
return Entries.contestEntries.get(position).entry_id;
}
@Override
public int getItemPosition(Object object) {
android.support.v4.app.Fragment f = (android.support.v4.app.Fragment)object;
for(int i = 0; i < getCount(); i++){
android.support.v4.app.Fragment fragment = getItem(i);
if(f.equals(fragment)){
return i;
}
}
return POSITION_NONE;
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
int entryId = getItemId(position);
if(Entries.mItems.get(entryId) != null) {
return Entries.mItems.get(entryId);
}
Fragment f = ScreenSlidePageFragment.create(position);
Entries.mItems.put(entryId, f);
return f;
}
@Override
public int getCount() {
return Entries.contestEntries.size();
}
}
as you can see, I'm just referencing static properties on my activity (Entries) because when I tried pushing those into members of the adapter, it wouldn't work at all.
here are the relevant properties from that activity:
public static ViewPager mPager;
public static PagerAdapter mPagerAdapter;
public static HashMap<Integer, android.support.v4.app.Fragment> mItems = new HashMap<Integer, android.support.v4.app.Fragment>();
public static int CurrentEntryId;
public static ArrayList<ContestEntryModel> contestEntries;
When the activity receives a new order, I'm currently just doing an mItems.clear() before I call notifyDataSetChanged(). This works fine but I have two issues with it: 1) it causes the screen to flash as it rebuilds the current fragment and 2) this is using more resources that I probably need to be using as I should be able to just use fragments that have already been built.
the way I'm resetting contestEntries is by receiving a JSON string from the web service then gson'ing it to my model, then resetting the contestEntries ArrayList to that new value.
I've also tried reordering the HashMap before calling notifyDataSetChanged() and that didn't work.
I have no problems changing this solution to use FragmentPagerAdapter() from FragmentStatePagerAdapter() if that's what's required to make this work, but as I said above, in the testing I did yesterday, it was not reordering the fragments using the solution posted above.
any suggestions would be greatly appreciated.
TIA