Swapping fragments in a viewpager

情到浓时终转凉″ 提交于 2019-12-08 09:30:55

问题


I have a ViewPager with 3 Fragments and my FragmentPagerAdapter:

private class test_pager extends FragmentPagerAdapter {
    public test_pager(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        return fragments[i];
    }

    @Override
    public long getItemId(int position) {
        if (position == 1) {
            long res = fragments[position].hashCode()+fragment1_state.hashCode();
            Log.d(TAG, "getItemId for position 1: "+res);
            return res;
        } else
            return fragments[position].hashCode();
    }

    @Override
    public int getCount() {
        return fragments[2] == null ? 2 : 3;
    }

    @Override
    public int getItemPosition(Object object) {
        Fragment fragment = (Fragment) object;
        for (int i=0; i<3; i++)
            if (fragment.equals(fragments[i])){
                if (i==1) {
                    return 1; // not sure if that makes a difference
                }
                return POSITION_UNCHANGED;
            }

        return POSITION_NONE;
    }
}

In one of the page (#1), I keep changing the fragment to be displayed. The way I remove the old fragment is like this:

FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().remove(old_fragment1).commit();

And then just changing the value of fragments[1]

I found that I cannot really add or replace the new one or it will complain the ViewPager is trying to add it too with another tag... (am I doing something wrong here?)

All the fragments I display have setRetainInstance(true); in their onCreate function.

My problem is that this usually works well for the first few replacement, but then when I try to reuse a fragment, sometimes (I have not really figured out the pattern, the same fragment may be displayed several times before this happens) it will only show a blank page.

Here is what I have found happened in the callback functions of my Fragment I am trying to display when the problem happens:

  • onAttach is called (but at that time, getView is still null)
  • onCreateView is not called (that's expected)
  • onViewStateRestored is not called (why not?)
  • onResume is not called (I really thought it would...)

If it changes anything, I am using the support package, my activity is a SherlockFragmentActivity

EDIT (to answer Marco's comment):

The fragments are instantiated in the onCreate function of the Activity, I fill an ArrayList with those fragments:

char_tests = new ArrayList<Fragment>(Arrays.asList(
            new FragmentOptionA(), new FragmentOptionB(), new FragmentOptionC()));

The I pick from that list to set fragments[1] (that's all done in the UI thread)


回答1:


I fixed this by changing test_pager to extends FragmentStatePagerAdapter instead.

I am still confused as to what PagerAdapter should be used depending on the usage. The only thing I can find in the documentation says that FragmentPagerAdapter is better for smaller number of pages that would be kept in memory and FragmentPagerStateAdapter better for a larger number of pages where they would be destroyed and save memory...

When trying to do (fancy?) things with Fragments, I found FragmentStatePagerAdapter is better when pages are removed and re-inserted like in this case. And FragmentPagerAdapter is better when pages move position (see bug 37990)



来源:https://stackoverflow.com/questions/13391048/swapping-fragments-in-a-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!