Search code examples
androidandroid-recyclerviewandroid-collapsingtoolbarlayoutandroid-appbarlayout

Android AppBarLayout + CollapsingToolbarLayout + CoordinatorLayout


Here is my layout hierarquy

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

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:theme="@style/AppThemeAppBarOverlay"
        app:elevation="0dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            contentScrim="@color/transparent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipChildren="false"
            android:clipToPadding="false"
            app:contentScrim="@color/transparent"
            app:layout_scrollFlags="scroll|enterAlways">

            <LinearLayout
                android:id="@+id/openday_parent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipChildren="false"
                android:clipToPadding="false"
                android:orientation="vertical"
                android:paddingTop="?attr/actionBarSize"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="1">
                ...</LinearLayout>

            <android.support.v7.widget.Toolbar 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:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:clipChildren="false"
                android:clipToPadding="false"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="snap|exitUntilCollapsed"
                app:popupTheme="@style/AppThemePopupOverlay" />

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/card_view_margin_bt"
        android:background="@color/windowBackground"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:listitem="@layout/card_item" />

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

<include layout="@layout/navigation_view" />

I have two problems:

  • When the appbarLayout is expanded, and i perform a quick scroll Up in the recyclerView, the recyclerView scroll really fast and at the same time the collapsing toolbar start collapsing. This shouldnt happen. I want to only allow scroll in the recyclerView when the collapsingLayout is fully collapsed. I think the problem has to do with the recyclerView fling, because if i scroll it slow, this bug doesn't occur. I'm trying to find a workaround for this.

  • The second thing is, when the activity start, the appbar is expanded. i want it to start collapsed, which works with

appBarLayout.setExpanded(false, true)

But with this approach, the toolbar is also collapsed. I want to just collapse the appBarLayout header but not the toolbar.


Solution

  • The first issue is related to clipToPadding messing with the recyclerView scrolls. Just removed it and the glitch was gone.

    The second issue was solved by removing all the toolbar flags and manually translating the view in the appBarLayout listener. Don't know if it is the best thing to do, but worked for me wonderfully.