How to add two fragments together in a frameLayout in runtime

情到浓时终转凉″ 提交于 2019-12-20 03:18:11

问题


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.

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?


回答1:


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

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



来源:https://stackoverflow.com/questions/17634178/how-to-add-two-fragments-together-in-a-framelayout-in-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!