How to disable BottomSheetDialogFragment dragging

后端 未结 14 1291
猫巷女王i
猫巷女王i 2021-01-31 04:58

How to disable BottomSheetDialogFragment dragging by finger?

I saw similar questions, but they\'re all about BottomSheet not BottomSheetD

14条回答
  •  一整个雨季
    2021-01-31 05:38

    My version. It works perfectly.

    Edit 09/04/2020: Replaced depreciated setBottomSheetCallback() with addBottomSheetCallback()

    class FragMenuBDrawer : BottomSheetDialogFragment() {
    
        ...
    
        override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
            val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
    
            dialog.setOnShowListener {
                val bottomSheet = (it as BottomSheetDialog).findViewById(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) {}
                })
            }
    
            // Do something with your dialog like setContentView() or whatever
            return dialog
        }
    
        ...
    }
    

提交回复
热议问题