Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 两个碎片代替了一个_Android_Android Layout_Android Fragments - Fatal编程技术网

Android 两个碎片代替了一个

Android 两个碎片代替了一个,android,android-layout,android-fragments,Android,Android Layout,Android Fragments,我有必要将一个活动的一个起始片段(我称之为A)替换为另外两个片段(B和C,在“常用”列表+查看器配置中)。目前,我有一个相对布局,其中两个框架布局充当B和C的占位符: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> &

我有必要将一个活动的一个起始片段(我称之为A)替换为另外两个片段(B和C,在“常用”列表+查看器配置中)。目前,我有一个相对布局,其中两个框架布局充当B和C的占位符:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RadioGroup
        android:id="@+id/radiogroup_navigation"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <!-- Some radiobuttons (not displayed for the sake of brevity) -->
    </RadioGroup>

<FrameLayout
    android:id="@+id/frame_list"
    android:layout_width="100dp"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/radiogroup_navigation">
</FrameLayout>

<FrameLayout
    android:id="@+id/frame_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/radiogroup_navigation"
    android:layout_toRightOf="@id/frame_list">
 </FrameLayout>
这样,当我按下后退按钮时,C和B都消失了,我回到了一个片段,但是现在帧列表可见(并且是空的)

我想用两种可能的方法来解决这个问题:

  • 如果需要,覆盖onBackPressed以隐藏左侧帧
  • 将B和C嵌套在另一个片段中

但我也觉得我可能用错误的方式看待这个问题,也许有一个更干净的设计解决方案。你有什么建议吗

如果我理解正确,这里有一个解决方案:

  • 创建两个活动ActivityA和ActivityBC
  • 使用放射组创建另一个碎片
  • 将FragmentRadio嵌入ActivityA和ActivityBC
  • 让该片段在完成当前活动的同时,根据选择开始新的活动
  • 创建如下字段:

    private static final String FRAGMENT_B_TAG = "fragmentB";
    
    添加片段时,请使用静态
    字符串
    s like标记:

    t.add(R.id.frame_list, fragmentB, FRAGMENT_B_TAG);
    t.add(R.id.frame_view, fragmentC, FRAGMENT_C_TAG);
    
    在活动中,设置一个侦听器,每次调用
    addToBackStack(String)
    后都会触发该侦听器。它将找出当前可见的片段,并隐藏/显示所需的容器

    getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            FragmentA fa = getSupportFragmentManager().findFragmentByTag(FRAGMENT_A_TAG);
            FragmentB fb = getSupportFragmentManager().findFragmentByTag(FRAGMENT_B_TAG);
            if (fa != null && fa.isVisible()) {
                // Fragment A is visible, so hide the second container which is now empty
            }
            if (fb != null && fb.isVisible()) {
                // Fragment B is visible, so show the second container
            }
        }
    });
    
    请注意,不需要检查
    片段C
    是否可见,因为当
    片段B
    可见时,
    片段C
    也始终可见

    这是一个未经测试的代码,但我认为它应该可以工作。此外,如果你需要任何解释,请毫不犹豫地询问

    希望能有帮助

    getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            FragmentA fa = getSupportFragmentManager().findFragmentByTag(FRAGMENT_A_TAG);
            FragmentB fb = getSupportFragmentManager().findFragmentByTag(FRAGMENT_B_TAG);
            if (fa != null && fa.isVisible()) {
                // Fragment A is visible, so hide the second container which is now empty
            }
            if (fb != null && fb.isVisible()) {
                // Fragment B is visible, so show the second container
            }
        }
    });