防止在触摸外部时解雇BottomSheetDialogFragment [英] Prevent dismissal of BottomSheetDialogFragment on touch outside
问题描述
我已经实现了一个BottomSheet对话框,并且我想防止在用户窥视底片外部(未完全展开状态)时关闭该底片.
I have implemented a BottomSheet Dialog and I want to prevent the bottomsheet from dismissing when the user touches outside of the bottomsheet when it's peeking (Not fully expanded state).
我在代码中设置了dialog.setCanceledOnTouchOutside(false);
,但似乎没有任何影响.
I have set dialog.setCanceledOnTouchOutside(false);
in the code but it doesn't seem to take any affect.
这是我的BottomSheetDialogFragment类:
Here's my BottomSheetDialogFragment class:
public class ShoppingCartBottomSheetFragment extends BottomSheetDialogFragment {
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
};
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.fragment_shopping_cart_bottom_sheet, null);
dialog.setCanceledOnTouchOutside(false);
dialog.setContentView(contentView);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
if( behavior != null && behavior instanceof BottomSheetBehavior ) {
((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
((BottomSheetBehavior) behavior).setPeekHeight(97);
((BottomSheetBehavior) behavior).setHideable(false);
}
}
@Override
public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
WindowManager.LayoutParams windowParams = window.getAttributes();
windowParams.dimAmount = 0;
windowParams.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(windowParams);
}
}
根据BottomSheet 规范底页可以可以通过触摸底部表格的外部来将其关闭,因此我有什么选择可以替代此行为并防止其被关闭?
According to the BottomSheet specification bottom sheets can be dismissed by touching outside of the bottom sheet, therefore what are my options to override this behavior and prevent it being dismissed?
推荐答案
在创建实例时应使用#setCancelable(false)
.
BottomSheetDialogFragment bottomSheetDialogFragment = new SequenceControlFragmentBottomSheet();
bottomSheetDialogFragment.setCancelable(false);
bottomSheetDialogFragment.show(getChildFragmentManager(), bottomSheetDialogFragment.getTag());
这篇关于防止在触摸外部时解雇BottomSheetDialogFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!