How to disable BottomSheetDialogFragment dragging

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

    Top rated answer contains boilerplate code such as Field and try-catches.

    So here is a better version of it in Kotlin:

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return super.onCreateDialog(savedInstanceState).apply {
            setOnShowListener(::onShow)
        }
    }
    
    private fun onShow(dialogInterface: DialogInterface) {
        val dialog = dialogInterface as BottomSheetDialog
        val frameLayout =
            dialog.findViewById(com.google.android.material.R.id.design_bottom_sheet)
                ?: return
    
        BottomSheetBehavior.from(frameLayout).apply {
            addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
                override fun onStateChanged(bottomSheet: View, newState: Int) {
                    if (newState == BottomSheetBehavior.STATE_DRAGGING)
                        state = BottomSheetBehavior.STATE_EXPANDED
                }
    
                override fun onSlide(bottomSheet: View, slideOffset: Float) = Unit
            })
        }
    }
    

    Use it in BottomSheetDialogFragment

提交回复
热议问题