Can't drag BottomSheet when clickable="true"? If not set clickable then click goes through it and fire on Recycleview item under it

Viewed 105

Here is my Persistent BottomSheet

<androidx.constraintlayout.widget.ConstraintLayout 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/bs"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bottom_sheet_background"
    android:elevation="2dp"

    android:padding="@dimen/base_margin"
    app:behavior_hideable="true"
    app:behavior_peekHeight="@dimen/bottom_sheet_peek_height"
    app:layout_behavior="@string/bottom_sheet_behavior">

When User scrolls RecycleView, BottomNavigation hides and I reduce height of BottomSheet accordingly in RecycleView's addOnScrollListener using:

binding.rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
    //148 = 80(bottom navigation) + 56(bottom sheet)
    if (dy < 0)//scroll down
        bottomSheetBehavior.setPeekHeight(136, true);
    else if (dy > 0)//scroll up
        bottomSheetBehavior.setPeekHeight(56, true);
}

After BottomNavigation is hidden and BottomSheet height is reduced, if BottomSheet is clickable,

(either through code binding.bs.bs.setClickable(false); or through xml android:clickable="true")

I can't drag it to expand. If it is not clickable, click event goes through it and user click on RecycleView item underneath it.

Even when its height is not reduced and it isn't clickable then also click event goes under it and fire on RecycleView item.

I also tried setting nestedScrolling, which allowed expanding but after that start creating issues when collapsing. :(

UPDATE: I noticed BottomSheet drag not works when I set Bottomsheet clickable and its peekheight < 80 dp, ie the height of BottomNavigation.

Reference:

Why am I able to click "behind" the bottomsheet in Android?

1 Answers

I had same use case of having a recycler view inside a bottom sheet and here's what my Bottom sheet XML looks like. Please try to check if this works for you!

Apart from this, I did not actually get it why you want to reduce the height of the bottom sheet, as Bottom sheet has the behavior of self adjusting its height respect to the content inside it.

Please if possible share your use case so that it will be easier for the community to answer which gonna hit the bonsai. Thanks!

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:background="@color/transparent"
    android:layout_height="match_parent"
    android:paddingTop="50dp"
    android:id="@+id/rootLayout"
    >

    <include
        android:id="@+id/progress"
        layout="@layout/item_progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:id="@+id/rootView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bottom_sheet_rounded_background"
        android:backgroundTint="@color/background_gray"
        android:clipToPadding="true"
        android:orientation="vertical"
        app:layout_behavior="@string/bottom_sheet_behavior"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <RelativeLayout
            android:id="@+id/title_rl"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingVertical="16dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_marginStart="24dp"
                android:height="24sp"
                android:fontFamily="@font/silka_bold"
                android:text="@string/error_select_prescription"
                android:textColor="@color/text_color_semi_black"
                android:textSize="16sp" />

            <ImageView
                android:id="@+id/iv_close"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginEnd="16dp"
                android:src="@drawable/ic_close_btn_gray" />
        </RelativeLayout>

        <View
            android:id="@+id/view"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/btn_gray"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/title_rl" />

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:fillViewport="true"
            android:orientation="vertical">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerViewPrescription"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:paddingTop="@dimen/dimen_16dp"
                tools:itemCount="10" />
        </androidx.core.widget.NestedScrollView>

        <include
            android:id="@+id/btn_select_and_proceed"
            layout="@layout/sticky_footer_design_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Related