Skip to content Skip to sidebar Skip to footer

Android. Tablayout Inside A Tab Of Another Tablayout

Is it possible to have a tablayout inside a tab of another tablayout? I have created the following image for a better explanation. The slide movement desired is as it is described

Solution 1:

ok, so inside your fragment you have only TextView. Now for fragment_2 put another TabLayout and ViewPager (you don't need AppBar, and Coordinator, they will be already there from parent layout of Activity)

frag2.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.design.widget.TabLayout
        android:id="@+id/frag2_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
    <android.support.v4.view.ViewPager
        android:id="@+id/frag2_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"  />
</LinearLayout>

you have to create Frag2 Fragment , init with above layout inside OnCreateView and then for frag2_viewpager ViewPager class set another adapter for frag2a.xml and frag2b.xml Fragments only, they may have lets assume only TextViews or whatever. TabLayout will be placed above, inside Frag2 (and one in Activity as on pic)


Solution 2:

I know that this is not a direct answer to the question and that I might get penalized for this, but you should not nest tabs in tabs. Google uses design concepts called material design. One of the rules for tabs is not to nest them. Here is a link to the page: https://material.google.com/components/tabs.html#tabs-usage. I think that it is really important that we as developers follow this to make the Android platform as universal and as amazing as it can be.

Thanks, Oak


Solution 3:

Simple

Everything will remain same like you set ViewPager in Activity. Just you need to use getChildFragmentManager() in Fragment instead of getFragmentManager().

From getChildFragmentManager() documentation

Return a private FragmentManager for placing and managing Fragments inside of this Fragment.

From getFragmentManager() documentation

Return the FragmentManager for interacting with fragments associated with this fragment's activity.

You can see @this answer for complete code.


Post a Comment for "Android. Tablayout Inside A Tab Of Another Tablayout"