# 是BottomSheetLayout!!!!
不是bottomsheetdialog或者bottomsheetdialogfragment
不是bottomsheetdialog或者bottomsheetdialogfragment
以下回答参考 皆我百晓生、券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。
要实现底部滑块布局(BottomSheetLayout)在用户向下拖动时阻止关闭,请遵循以下步骤:
<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>
<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>
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) {
// 未实现
}
}
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”按钮并向下拖动滑块时,滑块将被禁止向下滑动。