How to disable BottomSheetDialogFragment dragging

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

    There is simpler way of achieving the same after material design 1.2.0 was released.

    https://developer.android.com/reference/com/google/android/material/bottomsheet/BottomSheetBehavior#setdraggable

    When calling from BottomSheetDialogFragment:

        override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
            val bottomSheetDialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
            bottomSheetDialog.setOnShowListener {
                val bottomSheet = bottomSheetDialog
                    .findViewById(com.google.android.material.R.id.design_bottom_sheet)
    
                if (bottomSheet != null) {
                    val behavior: BottomSheetBehavior<*> = BottomSheetBehavior.from(bottomSheet)
                    behavior.isDraggable = false
                }
            }
            return bottomSheetDialog
        }
    

    Or with styling:

        
    

    And then in onCreate of your dialog fragment:

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setStyle(DialogFragment.STYLE_NORMAL, R.style.SomeStyle)
        }
    

提交回复
热议问题