Satwinder Singh
Satwinder Singh

Reputation: 627

How to pin tablayout on top while scrolling

i want to pin tab layout on top while scrolling and toolbar should hide. So I have achieved this using following code but problem is, when application start and activity created it works fine(pinned on top while scroll) but moving to another fragment and comeback it is not pinning on top it just scrolling like the toolbar. I am using this on fragment and parent home activity has noActionbar theme. Can anybody help? Here is my xml file code:

<android.support.design.widget.CoordinatorLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/background"
  tools:context="com.plowns.droidapp.activites.HomeActivity">

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/gradient_horizontal"
        android:contentInsetEnd="0dp"
        android:contentInsetLeft="0dp"
        android:contentInsetRight="0dp"
        android:contentInsetStart="0dp"
        app:contentInsetEnd="0dp"
        app:contentInsetLeft="0dp"
        app:contentInsetRight="0dp"
        app:contentInsetStart="0dp"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:id="@+id/rl_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="10dp"
                android:gravity="center_vertical">


                </RelativeLayout>

            </RelativeLayout>

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:elevation="8dp"
        android:gravity="bottom"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/colorPrimary"
        app:tabIndicatorHeight="3dp"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/white"
        app:tabTextColor="@color/white" />

</android.support.design.widget.AppBarLayout>

Upvotes: 1

Views: 2074

Answers (1)

devgun
devgun

Reputation: 1005

you have to set the app:layout_collapseMode to pin in TabLayout.

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:elevation="8dp"
    android:gravity="bottom"
    app:tabGravity="fill"
    app:tabIndicatorColor="@color/colorPrimary"
    app:tabIndicatorHeight="3dp"
    app:tabMode="scrollable"
    app:tabSelectedTextColor="@color/white"
    app:layout_collapseMode="pin"
    app:tabTextColor="@color/white" />

Upvotes: 2

Related Questions