AppBarLayout preventing RecyclerView from scrolling in BottomSheet

Viewed 408

I made a simple layout in which there is a AppBarLayout (that doesn't scroll), some content in the middle, and a BottomSheet. This BottomSheet is actually a LinearLayout with a BottomSheetBehavior and inside has a RecyclerView.

This BottomSheet when expanded places the RecyclerView on top of the AppBarLayout. The problem is that, when the user tries to scroll this RecyclerView, the AppBarLayout below steals the scroll.

I'm leaving the layout code, but I uploaded the whole example project to GitHub with a GIF to illustrate.

Layout

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:liftOnScroll="false">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:title="@string/app_name" />

        <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:background="?colorSurface"
            android:gravity="center_vertical"
            android:padding="16dp"
            android:text="Subtitle"
            android:textAppearance="?textAppearanceSubtitle1" />

        <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:background="?colorSurface"
            android:gravity="center_vertical"
            android:padding="16dp"
            android:text="Subsubtitle"
            android:textAppearance="?textAppearanceBody2" />

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

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="56dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:text="@string/lorem_ipsum"
            android:textAppearance="?textAppearanceBody1" />

    </androidx.core.widget.NestedScrollView>

    <LinearLayout
        android:id="@+id/bottom_sheet_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        android:background="#FFF"
        android:elevation="5dp"
        android:orientation="vertical"
        app:behavior_hideable="false"
        app:behavior_peekHeight="64dp"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/bottom_sheet_header"
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:gravity="center_vertical"
            android:padding="16dp"
            android:text="Fruits"
            android:textAppearance="?textAppearanceHeadline6" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

    </LinearLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

I've tried:

  • Placing the nestedScrollingEnabled attribute in various places, but didn't work and this behavior occurs in old APIs (like JellyBean);
  • In the original project where I saw this I wrote a custom AppBarLayout behavior to ignore if the target View is the RecyclerView of the BottomSheet, but also didn't work;
  • Set the whole AppBarLayout GONE when the BottomSheet is expanded and funny enough the AppBarLayout behaves like I set to INVISIBLE (if programmatically set to GONE, it behaves like invisible, if set before inflating, it behaves as expected).

I'm avoiding use a Fragment to create this BottomSheet due to project specifications.

1 Answers

The workaround I did was split the CoordinatorLayout in 2, where one contains the AppBarLayout and the NestedScrollView, and the other one contains the BottomSheet (LinearLayout).

The hierarchy, using the layout of the question as example, end up like this:

<FrameLayout>
    <CoordinatorLayout>
        <AppBarLayout>
            <MaterialToolbar/>
            <AppCompatTextView/>
            <AppCompatTextView/>
        </AppBarLayout>
        <NestedScrollView>
            <AppCompatTextView/>
        </NestedScrollView>
    </CoordinatorLayout>

    <CoordinatorLayout>
        <LinearLayout>
            <AppCompatTextView/>
            <RecyclerView/>
        </LinearLayout>
    </CoordinatorLayout>
</FrameLayout>

In my case this works because my BottomSheet doesn't need to coordinate any behavior with the other views.

Related