简体   繁体   中英

Nesting a Tablayout With Fragments as Child fragments inside an Fragment from Navigation drawer

I have implemented a Navigation drawer with Fragments Frag_A, Frag_B, Frag_C and Frag_D successfully, But I want inside Fragment B to put A Tablayout with 3 TABS (Child Fragments) child_Frag_1, child_Frag_2, child_Frag_3

Any Links on how to succesfully achive this with child_Frag_1, child_Frag_2 and child_Frag_1 displaying the content

You can use TabLayout as parent to your TabItems with FrameLayout to hold the actual fragment:

<android.support.design.widget.TabLayout  
        android:id="@+id/tabLayout"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:background="#7367">  
  
        <android.support.design.widget.TabItem  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="Home" />  
  
        <android.support.design.widget.TabItem  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="Java" />  
  
        <android.support.design.widget.TabItem  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="Android" />  
  
        <android.support.design.widget.TabItem  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="Php" />  
    </android.support.design.widget.TabLayout>  
  
    <FrameLayout  
        android:id="@+id/frameLayout"  
        android:layout_width="match_parent"  
        android:layout_height="455dp"  
        app:layout_constraintEnd_toEndOf="parent"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toBottomOf="@+id/tabLayout">  
  
    </FrameLayout>  
</android.support.constraint.ConstraintLayout>  

For better understanding and for the other detail on how you navigate through your tabs, see this simple tutorial that use the same thing you want to do, except the drawer, which make no problem, you can treat the fragment that host the TabLayout as normal activity like in this tutorial.

Note: I highly recommend you to use the new navigation component of Android Jetpack Architecture in this cases, it handle all the TROUBLE for you, like navigating and handling the backstack and a lot of other benfits, you can check the docs for more info about it.

Enjoy !

You can use the Adapters. You just need to create 1 layout and the adapter will reflect as many layouts or views as you want. You can learn a lot about adapters on the internet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM