Android ViewPager/FragmentStatePagerAdapter - Dynamically updated pages
have incorrect save states
I'm trying to use a FragmentStatePagerAdapter to hold an unknown (2-10)
amount of fragments in the ViewPager where the order and quantity can be
altered from the first fragment.
My main goal is that if I check 1, 2, 3 and then uncheck 2, I want the
ViewPager to display 1,3. If I re-check 2 it should display 1,3,2. (or
1,2,3 again... but it's not necessary). If there's a better way to get
this than what I've described below I'm open to it.
First I learned that I can't use FragmentPagerAdapter because once you
load a fragment into it, it's always going to be in the same position. So
when I unchecked 2, position 2 still showed 2, and not 3.
After a lot of searching I found that if i use the below code with
FragmentStatePagerAdapter, I can force it to reload all my fragments and
thus update the order. I'm not sure I like this method though as it always
has to reload my page each time and it can be visibly slow...
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
I've run into the problem now where I can keep the pages ordered
correctly, but the saved states are incorrect, it's not showing the
correct values when I return to a page after it's order is altered. The
toggles states (on/off) from the original page 2 are always shown on
whatever page 2 is being displayed, even if it's actually page 3, or 4
etc.
Potentially the issue is described below, indicating that
FragmentStatePagerAdapter doesn't reoder the save states even when the
fragments do change. I can't make out how to use his fix, I end up with my
fragments sometimes being blank as I move around when I drop his
NewFragmentStatePagerAdapter in place of Google's.
reorder pages in FragmentStatePagerAdapter using getItemPosition(Object
object)
This is my Adapter
public static class AppSectionsPagerAdapter extends
FragmentStatePagerAdapter {
private List<Fragment> mFragments = new ArrayList<Fragment>();
public List<Integer> mFragmentLocation = new ArrayList<Integer>();
int pageCount = 1;
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
//Loading my fragments into array.
Fragment fragment1 = new Fragment1();
Fragment fragment2 = new Fragment2();
Fragment fragment3 = new Fragment3();
mFragments.add(fragment1);
mFragments.add(fragment2);
mFragments.add(fragment3);
//Adding Home fragment into first position.
mFragmentLocation.add(Consts.HOMEFRAGMENT);
}
@Override
public int getItemPosition(Object item) {
return POSITION_NONE;
}
@Override
public Fragment getItem(int position) {
//Find out which fragment is in the current position, then get
that fragment.
return mFragments.get(mFragmentLocation.get(position));
}
@Override
public int getCount() {
return pageCount;
}
public int addPage() {
return pageCount++;
}
public int removePage() {
return pageCount--;
}
public void addTab(int fragmentType) {
//When a new tab is added, mark it's location so it can be
retrieved by getItem
mFragmentLocation.add(fragmentType);
}
public void removeTab(int fragmentType) {
//Find the location of my fragment in mFragmentLocation, and
remove it.
for (int j = 0; j < mFragmentLocation.size(); j++) {
if(mFragmentLocation.get(j) == fragmentType){
mFragmentLocation.remove(j);
Log.i("Remove Tab", "Removing " + fragmentType + " from
position " + j);
break;
}
}
}
//This doesn't work as expected yet with the adjusting fragments.
@Override
public CharSequence getPageTitle(int position) {
CharSequence pageTitle = null;
switch(position){
case 0:
pageTitle = "Fragment1";
return pageTitle;
case 1:
pageTitle = "Fragment2";
return pageTitle;
case 2:
pageTitle = "Fragment3";
return pageTitle;
}
Log.i("positoin","Position: "+position);
return pageTitle;
}
}
This is the function I use to remove my tabs which will force the adapter
to reload everything thus ensuring the order is correct.
public void removePageTabs(int fragmentType){
int listPosition = 0;
for (int j = 0; j < mAppSectionsPagerAdapter.mFragmentLocation.size();
j++) {
if(mAppSectionsPagerAdapter.mFragmentLocation.get(j) == fragmentType){
listPosition = j;
break;
}
}
//Remove the actionbar tab.
actionBar.removeTabAt(listPosition);
//Remove the fragment from the positional list.
mAppSectionsPagerAdapter.removeTab(fragmentType);
//Lower the page count in adapater.
mAppSectionsPagerAdapter.removePage();
//Notify of data change so my positions get updated.
mViewPager.getAdapter().notifyDataSetChanged();
}
No comments:
Post a Comment