谁都不许动我的砖 2020-02-05 20:58 采纳率: 0%
浏览 1141

如何设置BottomSheetLayout禁止向下滑动关闭

# 是BottomSheetLayout!!!!

不是bottomsheetdialog或者bottomsheetdialogfragment

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-26 03:07
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    要实现底部滑块布局(BottomSheetLayout)在用户向下拖动时阻止关闭,请遵循以下步骤:

    1. 在XML中定义底部滑块布局:
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <com.google.android.material.bottomsheet.BottomSheetLayout
            android:id="@+id/bottom_sheet_layout"
            app:layout_constraintEnd_toStartOf="@+id/vertical_margin"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/volume_button_container"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.5"
            app:layout_constraintVertical_top="0dp" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    1. 在布局文件中添加一个垂直空间来隔离滑块和按钮:
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:background="#ffffff"
        android:weightSum="3">
        <!-- 滑块 -->
        <Button
            android:id="@+id/volume_button"
            android:text="Volume"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <!-- 按钮 -->
        <Button
            android:id="@+id/button_close"
            android:text="Close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    </LinearLayout>
    
    1. BottomSheetLayout的子类中添加以下属性以禁用滑块向下滑动:
    public class CustomBottomSheetLayout extends androidx.appcompat.widget.AppCompatDialogFragment implements BottomSheetBehavior.BottomSheetCallback {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            if (newState == BottomSheetBehavior.STATE_DRAGGING || newState == BottomSheetBehavior.STATE_EXPANDED) {
                // 禁止滑块向下滑动
                bottomSheet.setLongClickable(false);
            }
        }
    
        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            // 未实现
        }
    }
    
    1. 在您的Activity或Fragment中使用这个定制的底部滑块布局,并在滑动事件监听器中调用 CustomBottomSheetLayout 的构造函数:
    @Override
    protected Dialog onCreateDialog(int id) {
        return new CustomBottomSheetLayout().setCustomView(getLayoutInflater().inflate(R.layout.custom_bottom_sheet_layout, null));
    }
    
    // 在滑动事件监听器中初始化底部滑块布局
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    
        // 初始化底部滑块布局
        BottomSheetBehavior behavior = BottomSheetBehavior.from((View) findViewById(R.id.bottom_sheet_layout));
        behavior.setBottomSheetCallback(this);
    }
    

    现在,当您点击“Close”按钮并向下拖动滑块时,滑块将被禁止向下滑动。

    评论

报告相同问题?