如何禁用BottomSheetDialogFragment拖动

Far*_*ABZ 13 android android-layout android-view android-dialog bottom-sheet

如何禁用BottomSheetDialogFragment手指拖动?

我看到了类似的问题,但他们BottomSheet都不是BottomSheetDialogFragment.

jak*_*ski 19

在 Material Design 1.2.0 发布后,有一种更简单的方法可以实现相同的目标。

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

从 调用时BottomSheetDialogFragment

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val bottomSheetDialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
        bottomSheetDialog.setOnShowListener {
            val bottomSheet = bottomSheetDialog
                .findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)

            if (bottomSheet != null) {
                val behavior: BottomSheetBehavior<*> = BottomSheetBehavior.from(bottomSheet)
                behavior.isDraggable = false
            }
        }
        return bottomSheetDialog
    }
Run Code Online (Sandbox Code Playgroud)

或者搭配造型:

    <style name="SomeStyle" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
        <item name="behavior_draggable">false</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

然后在onCreate你的对话片段中:

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


azi*_*ian 18

创建MyActivity如下:

public class MyActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        new MyBottomSheetFragment().show(getSupportFragmentManager(), "tag");
    }

    public static class MyBottomSheetFragment extends BottomSheetDialogFragment {

        @Override
        public void setupDialog(Dialog dialog, int style) {
            BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) dialog;
            bottomSheetDialog.setContentView(R.layout.sample);

            try {
                Field behaviorField = bottomSheetDialog.getClass().getDeclaredField("behavior");
                behaviorField.setAccessible(true);
                final BottomSheetBehavior behavior = (BottomSheetBehavior) behaviorField.get(bottomSheetDialog);
                behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {

                    @Override
                    public void onStateChanged(@NonNull View bottomSheet, int newState) {
                        if (newState == BottomSheetBehavior.STATE_DRAGGING{ 
                            behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                        }
                    }

                    @Override
                    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                    }
                });
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

R.layout.sample简单布局在哪里:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <View
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#e479da" />

    <View
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#798de4" />

    <View
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#e4db79" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

你会得到以下输出:

解决方案的一部分借鉴了这个答案.

  • 这是很好的示例代码,但存在一些问题。首先, setupDialog 不是 BottomSheetDialogFragment 方法。其次,我的底部工作表视图中有一个回收站视图,当我从 recyclerView 的空白区域拖动底部工作表时,底部工作表会向下滑动。 (2认同)

Dev*_*ngh 17

为时已晚,但值得分享。

  behavior.setDraggable(false)
Run Code Online (Sandbox Code Playgroud)

这条线完成了这项工作。


小智 13

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    //Disable dragging by set isDraggable to false
    val bottomSheetDialog = dialog as BottomSheetDialog
    val bottomSheetBehavior = bottomSheetDialog.behavior
    bottomSheetBehavior.isDraggable = false
}
Run Code Online (Sandbox Code Playgroud)


Sat*_*tli 7

我的版本。它完美地工作。

编辑2020年9月4日:替代已贬值setBottomSheetCallback()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
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)


小智 6

如果要禁用BottomSheetDialog拖动,请尝试设置setCancelable(false).

  • 它适用于我的情况,只是有一个问题,即在将可取消设置为 false 后,单击外部字段将被禁用。谢谢。+1 (4认同)

小智 5

在你的oncreateView

//Kotlin
val baseDialog = dialog
if (baseDialog is BottomSheetDialog) {
    baseDialog.behavior.isDraggable = false
}
//If cancelable also not required.
isCancelable = false
Run Code Online (Sandbox Code Playgroud)