/ / Android:いつ/なぜFragmentの代わりにFrameLayoutを使用する必要がありますか? -android、android-fragments、framelayout

Android:いつ/なぜ私はFrameLayoutをFragmentの代わりに使用する必要がありますか? - アンドロイド、アンドロイド - フラグメント、framelayout

大画面用のレイアウトを作成しています。これは、左部分と右部分の2つの異なる部分で構成されています。そのためには、2つのフラグメントを使用するのが正しい選択だと思いました。

次に、ナビゲーションの例を見ました マスター/詳細フロー。これは2ペインレイアウトで、右側がナビゲーション、左側が詳細ビューです。

しかし、その例では、私が期待していたものとは異なり、詳細ビューには FrameLayout それはその後保持します Fragment、代わりに Fragment 直接。

レイアウトXMLは次のようになります(例)。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".WorkStationListActivity" >

<fragment
android:id="@+id/workstation_list"
android:name="de.tuhh.ipmt.ialp.history.WorkStationListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@android:layout/list_content" />

<FrameLayout
android:id="@+id/workstation_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />

</LinearLayout>

私の質問は今:なぜですか FrameLayout の代わりに使用 Fragment 詳細ビューのためのそれ自体?理由または利点は何ですか?私もそれを使うべきですか?

回答:

回答№1の場合は36

詳細コンテナは FrameLayout なぜなら Fragment 表示されるものは、 FragmentTransaction"s replace() 方法。

の最初の引数 replace() フラグメントが置き換えられるコンテナのIDです。この例のFrameLayoutがFragmentに置き換えられた場合、両方とも WorkStationListFragment 現在表示されているフラグメントの詳細はすべて、新しいフラグメントに置き換えられます。 FragmentをFrameLayout内にカプセル化することにより、詳細だけを置き換えることができます。