web-dev-qa-db-ja.com

BottomSheetDialogFragmentのドラッグを無効にする方法

BottomSheetDialogFragment指によるドラッグを無効にする方法は?

私は同様の質問を見ましたが、それらはすべてBottomSheetではなくBottomSheetDialogFragmentに関するものです。

13
FarshidABZ

次のように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();
            }
        }
    }
}

どこ 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>

次の出力が得られます。

ソリューションの一部は this answerから借用しています。

15
azizbekian

これはKotlinバージョンの azizbekian's answer

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

BottomSheetDialogドラッグを無効にする場合は、setCancelable(false)を設定してください。

3
Ivan Platonov

私のバージョン。完璧に機能します。

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
    }

    ...
}
0
Sattar Hummatli