在viewpager交换片段 [英] Swapping fragments in a viewpager

查看:280
本文介绍了在viewpager交换片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个片段,我的一个FragmentPagerAdapter ViewPager:

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;
    }
}

在页面中的一个(#1),我不断改变片段被显示。我删除旧片段的方法是这样的:

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();

和则只是改变的值片段[1]

我发现我真的不能添加替换新一或会抱怨ViewPager试图用另一个标签添加它太...(我在这里做得不对?)

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?)

我所显示的片段具有 setRetainInstance(真); 在其的onCreate 函数

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 被称为(但在那个时候,getView仍然是空)

  • onCreateView 不叫(这是预期)

  • onViewStateRestored 不叫(为什么不呢?)

  • onResume 不叫(我真的认为它会...)

  • 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...)

如果它改变任何东西,我使用的支持包,我的活动是 SherlockFragmentActivity

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

修改(回答Marco的评论)

EDIT (to answer Marco's comment):

该片段在活动的OnCreate函数实例化,我填一个ArrayList与那些片段:

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()));

该我从列表中选择设置片段[1] (这是所有在UI线程中完成)

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

推荐答案

我解决了这个问题,通过改变test_pager到延伸的 FragmentStatePagerAdapter 来代替。

I fixed this by changing test_pager to extends FragmentStatePagerAdapter instead.

我仍然困惑,什么 PagerAdapter 应根据使用情况使用。我可以在文档中找到的唯一的事情说 FragmentPagerAdapter 为更好地的,将被保存在内存中的页面数量较少和 FragmentPagerStateAdapter 的页面数量较多,他们将被销毁,并节省内存更好的...

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...

在试图做(看中?)事情片段,我发现 FragmentStatePagerAdapter 是更好的被删除时,页面和重新插入像在这种情况下。而FragmentPagerAdapter是更好时,页面移动位置(见错误37990

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)

这篇关于在viewpager交换片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆