简体   繁体   English

当 clickable="true" 时不能拖动 BottomSheet? 如果未设置可点击,则单击通过它并在其下的 Recycleview 项目上触发

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

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:当用户滚动 RecycleView 时,BottomNavigation 隐藏,我在 RecycleView 的 addOnScrollListener 中相应地减少了 BottomSheet 的高度,使用:

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,隐藏BottomNavigation并降低BottomSheet高度后,如果BottomSheet是可点击的,

(either through code binding.bs.bs.setClickable(false); or through xml android:clickable="true" ) (通过代码binding.bs.bs.setClickable(false);或通过 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.如果它不可点击,点击事件通过它并且用户点击它下面的 RecycleView 项目。

Even when its height is not reduced and it isn't clickable then also click event goes under it and fire on RecycleView item.即使它的高度没有降低并且它不可点击,那么点击事件也会在它下方并在 RecycleView 项目上触发。

I also tried setting nestedScrolling, which allowed expanding but after that start creating issues when collapsing.我还尝试设置nestedScrolling,它允许展开但之后在折叠时开始产生问题。 :( :(

UPDATE: I noticed BottomSheet drag not works when I set Bottomsheet clickable and its peekheight < 80 dp, ie the height of BottomNavigation.更新:我注意到当我设置底部表可点击并且它的 peekheight < 80 dp 时,底部表拖动不起作用,即底部导航的高度。

Reference:参考:

Why am I able to click "behind" the bottomsheet in Android? 为什么我可以单击 Android 中底部表的“后面”?

I had same use case of having a recycler view inside a bottom sheet and here's what my Bottom sheet XML looks like.我有相同的用例,在底部表内有一个回收器视图,这就是我的底部表 XML 的样子。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM