T K
T K

Reputation: 31

RecyclerView inside BottomSheet does not scroll

I have to takeover an old projet with a big issue. The app contains BottomSheet displaying a huge list of elements.

Previously, that BottomSheet contained a NestedScrollView containing itself the RecyclerView. The problem was that the RecyclerView being positioned inside a NestedScrollView with nestedScrollingEnabled=false was loading the whole dataset and provoking an OutOfMemory crash.

I removed the NestedScrollView to let the RecyclerView make its recycling job but I ended up in a situation I cannot solve : I can't scroll inside my RecyclerView with gesture (I can use scrollToPosition however).

My bottom sheet layout is as follow

<?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/bottomSheetContainer"
app:layout_behavior="@string/bottom_sheet_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.bottomsheet.BottomSheetDragHandleView
            android:id="@+id/dragHandleView"
            style="?bottomSheetDragHandleStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent" />

        [...]

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/bottomSheetContent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="@dimen/medium"
            android:focusableInTouchMode="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tilSearch"
            tools:itemCount="3"
            tools:listitem="@layout/cell_guest" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Is there a way to fix my issue ? Am I doing something wrong ? Am I forgetting something ?

Upvotes: 0

Views: 36

Answers (1)

Umair Bashir
Umair Bashir

Reputation: 66

Reason: This usually happens because the BottomSheet itself is intercepting touch events instead of letting the RecyclerView handle them. So when you try to scroll, the BottomSheet tries to expand or collapse instead of allowing the RecyclerView to scroll properly.

How to Fix It?

Solution 1: Make sure your BottomSheet’s peek height is set properly, so the RecyclerView gets enough space to scroll:

app:behavior_peekHeight="300dp"

Solution 2: Since the BottomSheet can intercept scrolling events, you should enable nested scrolling on your RecyclerView in your Java/Kotlin code:

recyclerView.setNestedScrollingEnabled(true);

This ensures the RecyclerView can scroll inside the BottomSheet without conflicts.

Upvotes: 0

Related Questions