0

请看看这个模式。可能吗 ???

片段 A > 片段 B > 片段 C > 片段 D > 片段 E

索引 0 > 索引 1 > 索引 2 > 索引 3 > 索引 4

现在我想要什么:>>>

Fragment E切换到Fragment B而不删除Fragment D 和 C也不会再次添加 Fragment E。并且索引可能不会因此受到影响,因为如果从 B > C > D Press back else 我需要打开 Fragment E 将作为像以前一样工作。

谢谢

4

2 回答 2

0

如果您使用 ViewPager,则可以实现 ViewPager.OnPageChangeListener 并且在 iots onPageSelected 中,您将设置逻辑以使用 if else 更改页面

@Override
    public void onPageSelected(int position) {
        // set you logic here and set the relevant Fragment to the pager
}
于 2015-11-06T07:40:31.873 回答
0
if (fragmentA == null) {
    fragmentA = new AFragment();
    fragmentManager.beginTransaction().add(R.id.container1, fragmentA).commitAllowingStateLoss();
} else {
    if (fragmentA != null) 
        fragmentManager.beginTransaction().show(fragmentA).commit();
    if (fragmentB != null)
        fragmentManager.beginTransaction().hide(fragmentB).commit();
    if (fragmentC != null)
         fragmentManager.beginTransaction().hide(fragmentC).commit();
    if (fragmentD != null)
        fragmentManager.beginTransaction().hide(fragmentD).commit();
}

您可以尝试这样,如果 Fragment 为空,则声明它,然后根据您的目的显示或隐藏。container1 是这个的布局

<FrameLayout
    android:id="@+id/container1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/lnBottomButton" />

这是包含 Activity 中所有片段的主视图

于 2015-11-06T07:34:19.820 回答