How to disable BottomSheetDialogFragment dragging

后端 未结 14 1289
猫巷女王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:42

    This is the Kotlin version of azizbekian's answer since someone asked about using data binding

    @SuppressLint("RestrictedApi")
    override fun setupDialog(d: Dialog?, style: Int) {
        super.setupDialog(d, style)
        dialogExampleBinding = DataBindingUtil
            .inflate(LayoutInflater.from(context), R.layout.dialogExample, null, false) //This is for data binding only
        d?.setContentView(R.layout.dialogExample)
    
        val myDialog:BottomSheetDialog = d as BottomSheetDialog
        val dField = myDialog.javaClass.getDeclaredField("behavior") //This is the correct name of the variable in the BottomSheetDialog class
        dField.isAccessible = true
        val behavior = dField.get(d) as BottomSheetBehavior<*>
        behavior.setBottomSheetCallback(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) {}
        })
    }
    

提交回复
热议问题