我有必要用兩個其他片段(B和C,在「常用」列表+查看器配置中)替換一個活動的一個起始片段(我將其稱爲A)。目前我有兩個框架佈局中充當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>
當我需要顯示,我只是隱藏frame_list和A添加到frame_view ,並且當我需要再次顯示B和CI時,請設置frame_list可見並在同一個分段事務中將這兩個片段添加到每個幀。
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.remove(fragmentA);
t.add(R.id.frame_list, fragmentB);
t.add(R.id.frame_view, fragmentC);
t.addToBackStack(null);
t.commit();
這樣,當我按下後退按鈕時,C和B走開,我回片段,但現在frame_list是可見的(空)。
我想解決兩個可能的途徑問題:
- 首要onBackPressed如果需要隱藏左幀;
- 將B和C嵌套在另一個片段中;
但我也覺得我可能在錯誤的方式看問題,也許有一個更清潔的設計解決方案。你有什麼建議嗎?