Search code examples
androidandroid-layoutandroid-recyclerviewandroid-coordinatorlayoutcoordinator-layout

CoordinatorLayout with NestedScrollView and Horizontal Scrolling RecyclerView


I have a CoordinatorLayout with an AppBarLayout to collapse/expand the Toolbar on scroll. The content view is a NestedScrollView with RecyclerViews (which scroll horizontally) and some other views without scrollviews. Pretty similar to the Airbnb app.

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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">
        <include layout="@layout/toolbar_flat" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
            android:id="@+id/newstedScrollView"
           app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/consistentGreyWhite"
                    android:paddingBottom="@dimen/activity_vertical_margin"
                    android:clipToPadding="false"
                    android:orientation="vertical">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/home_slider"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/consistentWhite"
                        android:clipToPadding="false"
                        android:paddingBottom="@dimen/activity_vertical_margin"
                        android:paddingLeft="14dp"
                        android:paddingRight="14dp"
                        android:paddingTop="@dimen/activity_vertical_margin"
                        />

                    <... other elements ...>

            </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

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

If I scroll on an element without scrollviews the toolbar collapse/expand works. But if I perform a scroll (vertical) on a RecyclerView the toolbar doesn't work as expected. Seems like the RecyclerViews don't pass the scroll events to the CoordinatorLayout.


Solution

  • I too had this problem.

    Add this property to your NestedScrollView tag

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    

    Also, do the following for each recyclerView object in your fragment or activity. And also in your adapter if you are nesting recycler_views'.

    recyclerView.setNestedScrollingEnabled(false);