Search code examples
androidandroid-recyclerviewandroid-coordinatorlayoutandroid-nestedscrollview

How to use a RecyclerView in a CoordinatorLayout but without NestedScrollview ? (Scroll behaviors are not working)


I use a CollapsingToolbarLayout and a BottomAppBar reacting to scroll changes in a CoordinatorLayout (collapsing and hiding on scroll). As I can't use a NestedScrollview as parent of the RecyclerView because it leads to issues when I need to use scrollToPosition() or when an item is dragged out of the bounds (It doesn't scroll to move the item), the scroll behaviors are not working for the CollapsingToolbarLayout and the BottomAppBar.

I tried android:nestedScrollingEnabled="true" but it was not working

How can I keep the scroll-related behaviors in the CoordinatorLayout without NestedScrollview ?

<androidx.coordinatorlayout.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:fitsSystemWindows="true">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/Theme.Todolist.AppBarOverlay">     

        <com.google.android.material.appbar.CollapsingToolbarLayout
             android:id="@+id/toolbar_layout"
             android:layout_width="match_parent"
             android:layout_height="@dimen/app_bar_height"
             android:background="@color/colorPrimary"
             android:fitsSystemWindows="true"
             android:theme="@style/ThemeOverlay.MaterialComponents.ActionBar"
             app:contentScrim="@color/colorPrimary"
             app:layout_scrollFlags="exitUntilCollapsed|scroll"
             app:toolbarId="@+id/toolbar">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/Theme.Todolist.PopupOverlay"
            app:title="Mes tâches"
            app:titleTextColor="@color/iconTint"/>

        </com.google.android.material.appbar.CollapsingToolbarLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/tasks_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/background"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:clipToPadding="false"
        android:paddingBottom="32dp"/>

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppBar"
        style="@style/Widget.MaterialComponents.BottomAppBar.Colored"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:hideOnScroll="true"
        app:menu="@menu/bottom_app_bar"
        app:navigationIcon="@drawable/ic_menu_black_24dp" />
  
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/add_task_text"
        app:backgroundTint="@color/floatingAddButton"
        app:layout_anchor="@+id/bottomAppBar"
        app:layout_anchorGravity="top|center"
        app:maxImageSize="50dp"
        app:srcCompat="@drawable/ic_add_black_48dp"
        app:tint="@color/addIconTint" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Solution

  • Use android:nestedScrollingEnabled="true"