How to disable BottomSheetDialogFragment dragging

耗尽温柔 提交于 2019-12-31 10:41:57

问题


How to disable BottomSheetDialogFragment dragging by finger?

I saw similar questions, but they're all about BottomSheet not BottomSheetDialogFragment.


回答1:


Having created MyActivity as follows:

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();
            }
        }
    }
}

Where R.layout.sample is a simple layout:

<?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>

You'll get following output:

A part of solution is borrowed from this answer.




回答2:


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




回答3:


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) {}
    })
}



回答4:


My version. It works perfectly.

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.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) {}
            })
        }

        // Do something with your dialog like setContentView() or whatever
        return dialog
    }

    ...
}



回答5:


Just Add bottomSheetBehavior.setHideable(false);

You can get Object of BottomshitBehaviour in BottomshitDialogFragment.

 CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) view.getParent()).getLayoutParams();
            CoordinatorLayout.Behavior behavior = params.getBehavior();
            View parent = (View) view.getParent();
            getHeight(view);
            ((BottomSheetBehavior) behavior).setFitToContents(true);
            BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(parent);
bottomSheetBehavior.setHideable(false);


来源:https://stackoverflow.com/questions/46861306/how-to-disable-bottomsheetdialogfragment-dragging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!