Search code examples
androidandroid-recyclerviewandroid-collapsingtoolbarlayoutandroid-appbarlayout

Collapsing toolbar layout collapses when try to scroll it but not when try to scroll recyclerview


I am using an XML where there is a collapsing tollbar layout inside appbar layout and there is a recyclerview. Now I successfully can collapse and emarge the collapsing toolbar layout when i touch and scroll it. But similar situation does not occur when I touch and scroll the recyclerview.

Here is my code;

<?xml version="1.0" encoding="utf-8"?>
<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="#fff"
    tools:context=".HomeActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/AppBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:textAlignment="center"
            app:contentScrim="@color/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:title="Hero Title">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="#000000"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7" />

            <android.support.v7.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Dark">

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

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

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/notification_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>

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

I want the collapsing toolbar to collapse when scrolling the recyclerview. Not just when scrolling the collapsing toolbar itself.


Solution

  • add NestedScrollView to your layout after AppBarLayout

    .
    .
    .
    </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">
    .
    .
    .
    

    and in your java class add

    YourRecyclerView.setNestedScrollingEnabled(false);