0

I am trying to replace fragments from my ViewPager adapter but it does not seem to work. No errors do show either. When I'm calling the replace(int, Fragment) method it does nothing. Do you have any clue why it's nto working?

public class PagerAdapter extends FragmentPagerAdapter {
    private Vector<Fragment> mFragments;
    private Stack<Fragment> mHiddenFragments;
    public PagerAdapter(FragmentManager fm) {
        super(fm);
        mFragments = new Vector<Fragment>();
        mHiddenFragments = new Stack<Fragment>();
    }
// here is the replacement method I'm using
    public void replace(int index, Fragment fragment) { 
        mHiddenFragments.push(mFragments.get(index));
        mFragments.set(index, fragment);
        notifyDataSetChanged();
    }
    public void replace(int index) {
        mFragments.set(index, mHiddenFragments.pop());
        notifyDataSetChanged();
    }
    public void add(Fragment fragment) {
        mFragments.add(fragment);
        notifyDataSetChanged();
    public void remove(int index) {
        mFragments.remove(index);
        notifyDataSetChanged();
    }
}

EDIT: The part which I can't get to work is the removing of the fragment. It seems I can only remove fragments in the ViewPager that are after the one that's currently viewed.

example: Suppose I have fragments 0 & 1 => mFragments.size() == 2

mPagerAdapter.add(new GenreListFragment());
mPagerAdapter.remove(1);
mewa
  • 1,532
  • 1
  • 12
  • 20

2 Answers2

1

My favorite solution is using nested fragments 'cause that gives me full control of them.

You could also check this Replace Fragment inside a ViewPager

Edit. You can also implement the abstract method of PagerAdapter for when you get a notifyDataSetChanged() call.

@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}

Regards.

Community
  • 1
  • 1
Junior Buckeridge
  • 2,075
  • 2
  • 21
  • 27
  • I imagine it's not well-suited supposing I need replacements for more fragments, I need something more general. – mewa Dec 11 '13 at 22:34
  • But if you want to have multiple fragments per page you can create an "empty" container fragment used in the pager adapter, and then add the childs to those "base" fragments. – Junior Buckeridge Dec 11 '13 at 22:37
  • I've already managed to add several fragments per page it turned out, however, that it's not the solution I seek. – mewa Dec 11 '13 at 22:38
  • Can you explain a little bit more what you want? Right now I don't get the idea. – Junior Buckeridge Dec 11 '13 at 22:40
  • I've made quite a lot of changes to my project and tried returning POSITION_NONE and it suddenly started working, not sure what had I been doing wrong that it hadn't worked before, though. – mewa Dec 12 '13 at 20:36
1

Okay, I finally know how to fix it. I changed my PagerAdapter to extend FragmentStatePagerAdapter instead of FragmentPagerAdapter and only then when I overrode getItemPosition started working properly and my fragments were replaced correctly.

@Override
     public int getItemPosition(Object object) {
     return POSITION_NONE;
}

Also I discovered that FragmentStatePagerAdapter will apparently be better in my case too (many dynamically created fragments).

mewa
  • 1,532
  • 1
  • 12
  • 20