如何在运行时在 frameLayout 中添加两个片段 [英] How to add two fragments together in a frameLayout in runtime

查看:18
本文介绍了如何在运行时在 frameLayout 中添加两个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个带有 navigation drawer 的 andorid 应用程序,我有一个 contentview 的布局,如下所示:

I am developing an andorid application with navigation drawer and I have a layout for contentview like this:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:dividerHeight="0dp"
        android:divider="@android:color/transparent"
        android:background="@color/default_color"/>
</android.support.v4.widget.DrawerLayout>

所以我可以在导航抽屉项目点击时添加和替换片段.到目前为止一切正常.

So I can add and replace fragment on navigation drawer item click. Everything okay till now.

但在一种情况下,在单击导航抽屉上的项目时,我需要显示两个片段而不是一个片段(就像一个 listFragment 和 details 片段).我该怎么做?那么我应该在框架布局中添加两个片段吗?但是如何?还是应该使用不同的 drawerLayout 更改 contentView ?如果是,我该怎么做?

But in one case, on clicking an item on navigation drawer I need to show two fragments instead of one ( just like a listFragment and details fragment ). How can I do it? Should I add two fragments into the framelayout then? But how? or should I change the contentView with different drawerLayout ? If yes, how can I do this?

推荐答案

您可以只使用嵌套片段.创建另一个片段,该片段将动态创建您的片段并将它们添加到其布局中.

You may just use nested fragments. Create another fragment that will dynamically create both your fragments and add them to its layout.

欲了解更多信息,请阅读:http://developer.android.com/about/versions/android-4.2.html#NestedFragments

For more information read this: http://developer.android.com/about/versions/android-4.2.html#NestedFragments

这篇关于如何在运行时在 frameLayout 中添加两个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆