2017-02-10 97 views
3

我已經實現了一個BottomSheet對話框,並且當用戶在偷看時(不完全展開狀態),當用戶觸摸底部片材外部時,我想防止底部表單被解僱。防止在觸摸外面觸碰BottomSheetDialogFragment

我在代碼中設置了dialog.setCanceledOnTouchOutside(false);,但它似乎沒有任何影響。

這裏是我的BottomSheetDialogFragment類:

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 specification下部片可以通過觸摸下片外被開除,所以我有哪些選擇重寫此行爲,並防止它被解僱?

回答

5

創建實例時應使用#setCancelable(false)

BottomSheetDialogFragment bottomSheetDialogFragment = new SequenceControlFragmentBottomSheet(); 
    bottomSheetDialogFragment.setCancelable(false); 
    bottomSheetDialogFragment.show(getChildFragmentManager(), bottomSheetDialogFragment.getTag());