androidandroid-tablayoutandroid-collapsingtoolbarlayoutandroid-scrollable-tabs

Tabs in CollapsingToolbarLayout overlapping RecyclerView


I have a TabLayout which has a ViewPager that selects between two fragments which both have vertical scrolling RecyclerViews. I have a scrolling TabLayout so that when I scroll the RecyclerView, the tabs disappear. However, it starts off with the TabLayout overlapping the RecyclerView's first item, cutting it off. I have tried adding margin to the ViewPager but this margin exists even after the tabs have been scrolled off of the screen. How could I change it so the tabs do not overlap the first item of the RecyclerView?

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways">

            <android.support.design.widget.TabLayout
                android:id="@+id/sliding_tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabGravity="fill"
                app:tabMaxWidth="0dp"
                app:tabMode="fixed"
                app:tabPadding="5dp"/>

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

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

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


Solution

  • add layout_behavior to your view pager

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