How to disable BottomSheetDialogFragment dragging

后端 未结 14 1275
猫巷女王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<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) {}
                })
            }
    
            // Do something with your dialog like setContentView() or whatever
            return dialog
        }
    
        ...
    }
    
    0 讨论(0)
  • 2021-01-31 05:38

    This works for me sir

    override fun onCreateDialog(savedInstanceState : Bundle?) : Dialog {
        val dialog = super.onCreateDialog(savedInstanceState);
        dialog.setOnShowListener {
            val bottomSheetDialog : BottomSheetDialog = it as BottomSheetDialog;
            var bottomSheetBehavior = BottomSheetBehavior<FrameLayout>();
            bottomSheetBehavior = bottomSheetDialog.getBehavior()
            bottomSheetBehavior.setDraggable(false);
        }
        return dialog }
    
    0 讨论(0)
  • 2021-01-31 05:42

    If you want to disable BottomSheetDialog dragging, try to set setCancelable(false).

    0 讨论(0)
  • 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) {}
        })
    }
    
    0 讨论(0)
  • 2021-01-31 05:42

    This is my solution:

    setOnShowListener {
                Handler().post {
                    val bottomSheet = findViewById<View>(R.id.design_bottom_sheet) as? FrameLayout
                    bottomSheet?.let {
                        BottomSheetBehavior.from(it).state = STATE_EXPANDED
    
                        // Disable dialog dragging behavior which causes issue on EditText scroll!
                        BottomSheetBehavior.from(it).isDraggable = false
                    }
    
                }
            }
    
    0 讨论(0)
  • 2021-01-31 05:43

    I get the answer here, I just added content.setLayoutParams(new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT, CoordinatorLayout.LayoutParams.MATCH_PARENT)); to make Bottom Sheet Dialog Fragment height is match_parent and make it soft when it showed.

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Dialog d = super.onCreateDialog(savedInstanceState);
        // view hierarchy is inflated after dialog is shown
        d.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                //this disables outside touch
                d.getWindow().findViewById(R.id.touch_outside).setOnClickListener(null);
                //this prevents dragging behavior
                View content = d.getWindow().findViewById(R.id.design_bottom_sheet);
                content.setLayoutParams(new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT, CoordinatorLayout.LayoutParams.MATCH_PARENT));
                ((CoordinatorLayout.LayoutParams) content.getLayoutParams()).setBehavior(null);
            }
        });
        return d;
    }
    
    0 讨论(0)
提交回复
热议问题