Android:何时/为什么我应该使用FrameLayout而不是Fragment?

时间:2022-01-16 07:07:15

I am building a layout for large screens, that is supposed to consist of 2 different parts, a left one and a right one. For doing that I thought using 2 Fragments is the right choice.

我正在为大屏幕构建一个布局,它应该包含两个不同的部分,一个是左边的,一个是右边的。为此,我认为使用2片段是正确的选择。

Then I had a look on the example of the navigation with the Master/Detail-Flow. It has a 2-pane layout, where on the right is the navigation, and on the left is the detail view.

然后我看了一下使用Master / Detail-Flow导航的例子。它有一个2窗格的布局,右边是导航,左边是详细视图。

But in that example, different from what I expected to see, for the detail view there is a FrameLayout that then holds a Fragment, instead of a Fragment directly.

但是在那个例子中,与我期望看到的不同,对于细节视图,有一个FrameLayout然后直接保存Fragment而不是Fragment。

The layout XML looks like this (an example):

布局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>

My question now is: why is a FrameLayout used instead of the Fragment itself for the detail view? What is the reason or the advantage? Should I use it too?

我现在的问题是:为什么使用FrameLayout而不是片段本身用于细节视图?是什么原因或优势?我也应该使用它吗?

1 个解决方案

#1


36  

The detail container is a FrameLayout because the Fragment that is displayed will be replaced using FragmentTransaction's replace() method.

详细信息容器是FrameLayout,因为显示的Fragment将使用FragmentTransaction的replace()方法替换。

The first argument to replace() is the ID of container whose Fragments will be replaced. If the FrameLayout in this example were replaced with a Fragment, then both the WorkStationListFragment and whatever detail Fragment is currently shown would be replaced by the new Fragment. By encapsulating the Fragment within a FrameLayout, you can replace just the details.

replace()的第一个参数是将替换Fragments的容器的ID。如果此示例中的FrameLayout被片段替换,则当前显示的WorkStationListFragment和任何细节片段都将被新片段替换。通过将Fragment封装在FrameLayout中,您可以只替换细节。

#1


36  

The detail container is a FrameLayout because the Fragment that is displayed will be replaced using FragmentTransaction's replace() method.

详细信息容器是FrameLayout,因为显示的Fragment将使用FragmentTransaction的replace()方法替换。

The first argument to replace() is the ID of container whose Fragments will be replaced. If the FrameLayout in this example were replaced with a Fragment, then both the WorkStationListFragment and whatever detail Fragment is currently shown would be replaced by the new Fragment. By encapsulating the Fragment within a FrameLayout, you can replace just the details.

replace()的第一个参数是将替换Fragments的容器的ID。如果此示例中的FrameLayout被片段替换,则当前显示的WorkStationListFragment和任何细节片段都将被新片段替换。通过将Fragment封装在FrameLayout中,您可以只替换细节。