第一种、将dialog的高度和behavior的setPeekHeight的高度设置为一致的时候,就可以让该控件不可拖拽
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
bind = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.dialog_car_often_group, null, false)
val wm = activity!!.windowManager
val height = wm.defaultDisplay.height
mMaxSlideHeight = (0.85f * height).toInt()
dialog.setOnShowListener {
val bottomSheet = (it as BottomSheetDialog).findViewById<View>(com.google.android.material.R.id.design_bottom_sheet) as FrameLayout?
val behavior = BottomSheetBehavior.from(bottomSheet!!)
behavior.state = BottomSheetBehavior.STATE_EXPANDED
//设置peek高度
behavior.setPeekHeight(mMaxSlideHeight)
//是否下拉关闭dialog
//behavior.setHideable(false)
}
if (mMaxSlideHeight > 0) {
//设置dialog高度
val params =
ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
mMaxSlideHeight)
dialog.setContentView(bind.root, params)
} else {
val params = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
dialog.setContentView(bind.root, params)
}
// Do something with your dialog like setContentView() or whatever
return dialog
}
第二种、设置addBottomSheetCallback监听,在onStateChanged的状态变化时都设置为展开状态,同时要设置最大高度。但是这样就会导致下拉关闭功能失效
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
bind = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.dialog_car_often_group, null, false)
val wm = activity!!.windowManager
val height = wm.defaultDisplay.height
val mMaxSlideHeight = (0.55f * height).toInt()
dialog.setOnShowListener {
val bottomSheet = (it as BottomSheetDialog).findViewById<View>(com.google.android.material.R.id.design_bottom_sheet) as FrameLayout?
val behavior = BottomSheetBehavior.from(bottomSheet!!)
behavior.state = BottomSheetBehavior.STATE_EXPANDED
behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_DRAGGING) {
behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {}
})
behavior.setHideable(false)
}
if (mMaxSlideHeight > 0) {
val params =
ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mMaxSlideHeight)
dialog.setContentView(bind.root, params)
} else {
val params = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
dialog.setContentView(bind.root, params)
}
// Do something with your dialog like setContentView() or whatever
return dialog
}
|