Skip to content Skip to sidebar Skip to footer

Launching New Fragment From Fragment In Tab Layout Doesn't Hide The Tabs

So I have implemented Tab layout with ViewPager in my app. Each tab shows a fragment. Now I want to open a new fragment(lets call it B) from one of these fragments in the tab layou

Solution 1:

I'm assuming that you are successfully being able to replace from FragA to FragB.

My suggestion is -> put your TabLayout into AppBarLayout,set properties of your AppBarLayout as android:animateLayoutChanges="true".

Later programmatically hide your TabLayout using setVisibility(View.GONE) when replacing your fragment from A->B.


Solution 2:

I think a better way would be to remove the TabLayout and ViewPager from the Activity and put them inside a top level level Fragment. Inside this top level fragment use the ChildFragmentManager to populate the ViewPager.

This way whenever you want to launch a new Fragment(B in your case) from any of the existing fragments then you can use your Activity's FrameLayout container to replace this whole ViewPager and TabLayout containing Fragment(A in your case) with a brand new Fragment(B). As the Activity does not contain the ViewPager and the Tablayout, they wont be visible in your new Fragment.

Have a look at this answer for a similar approach - https://stackoverflow.com/a/29315649/4440874

Activity structure should be like this -

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<FrameLayout 
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Fragment A layout -

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout"/>

Post a Comment for "Launching New Fragment From Fragment In Tab Layout Doesn't Hide The Tabs"