BottomSheetDialogFragment - 听取用户事件解雇

pro*_*m85 19 android dialog android-dialogfragment dialogfragment bottom-sheet

我如何听取最后解雇BottomSheetDialogFragment?我想在最终解雇时保存用户更改...

我试过以下:

方法1

如果通过向下滑动对话框解除对话(不在背面按压或外面触摸),则仅触发此操作

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    Dialog d = super.onCreateDialog(savedInstanceState);
    d.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {

            BottomSheetDialog d = (BottomSheetDialog) dialog;   
            FrameLayout bottomSheet = (FrameLayout) dialog.findViewById(android.support.design.R.id.design_bottom_sheet);

            BottomSheetBehavior behaviour = BottomSheetBehavior.from(bottomSheet);
            behaviour.setState(BottomSheetBehavior.STATE_EXPANDED);
            behaviour.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                @Override
                public void onStateChanged(@NonNull View bottomSheet, int newState) {
                    if (newState == BottomSheetBehavior.STATE_HIDDEN)
                    {
                        // Bottom Sheet was dismissed by user! But this is only fired, if dialog is swiped down! Not if touch outside dismissed the dialog or the back button
                        Toast.makeText(MainApp.get(), "HIDDEN", Toast.LENGTH_SHORT).show();
                        dismiss();
                    }
                }

                @Override
                public void onSlide(@NonNull View bottomSheet, float slideOffset) {

                }
            });
        }
    });
    return d;
}
Run Code Online (Sandbox Code Playgroud)

方法2

这不允许我区分最终解雇和来自屏幕轮换或活动娱乐的解雇......

 @Override
public void onDismiss(DialogInterface dialog)
{
    super.onDismiss(dialog);
    // this works fine but fires one time too often for my use case, it fires on screen rotation as well, although this is a temporarily dismiss only
    Toast.makeText(MainApp.get(), "DISMISSED", Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud)

如何收听表示用户已完成对话的事件?

pro*_*m85 37

虽然关于SO的所有类似问题建议使用onDismiss我认为以下是正确的解决方案:

@Override
public void onCancel(DialogInterface dialog)
{
    super.onCancel(dialog);
    Toast.makeText(MainApp.get(), "CANCEL", Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud)

如果:

* the user presses back
* the user presses outside of the dialog
Run Code Online (Sandbox Code Playgroud)

这不会引发:

* on screen rotation and activity recreation
Run Code Online (Sandbox Code Playgroud)

结合onCancelBottomSheetBehavior.BottomSheetCallback.onStateChanged如下:

public class Dailog extends BottomSheetDialogFragment
{
    @Override
    public void onCancel(DialogInterface dialog)
    {
        super.onCancel(dialog);
        handleUserExit();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        Dialog d = super.onCreateDialog(savedInstanceState);
        d.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                BottomSheetDialog d = (BottomSheetDialog) dialog;
                FrameLayout bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet);
                BottomSheetBehavior behaviour = BottomSheetBehavior.from(bottomSheet);
                behaviour.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                    @Override
                    public void onStateChanged(@NonNull View bottomSheet, int newState) {
                        if (newState == BottomSheetBehavior.STATE_HIDDEN)
                        {
                            handleUserExit();
                            dismiss();
                        }
                    }

                    @Override
                    public void onSlide(@NonNull View bottomSheet, float slideOffset) {

                    }
                });
            }
        });
        return d;
    }

    private void handleUserExit()
    {
        Toast.makeText(MainApp.get(), "TODO - SAVE data or similar", Toast.LENGTH_SHORT).show();
    }
}
Run Code Online (Sandbox Code Playgroud)


Gas*_*lén 7

如果您从BottomSheetDialogFragment()扩展,只需在您的类中覆盖

 override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        //Code here
    }
Run Code Online (Sandbox Code Playgroud)

这将在onBackPress时触发,以及当您通过单击对话框外部关闭对话框时触发。

确保不要将您的对话框设置为可取消,因为这不会触发


Ras*_*d.Z 5

我使用这个简单的技巧实现了这一点

val bottomSheetDialog = FeedbackFormsFragment.createInstance()
bottomSheetDialog.show((activity as FragmentActivity).supportFragmentManager, BOTTOM_SHEET)


// add some delay to allow the bottom sheet to be visible first so that the dialog is not null

                Handler().postDelayed({
                    bottomSheetDialog.dialog?.setOnDismissListener {

                       // add code here
                    }
                }, 1000)
Run Code Online (Sandbox Code Playgroud)


Coo*_*ind 5

虽然 @prom85 的方法有效,但还有一种不同的方法。如果你想BottomSheetDialogFragment在一种情况下解雇 a 而在另一种情况下保留,这是行不通的。在所有情况下它将关闭对话框。

例如,如果您在内部输入文本EditTextBottomSheetDialogFragment偶尔在外部单击,则会关闭对话框,而不会发出任何警告。我尝试了https://medium.com/@anitas3791/android-bottomsheetdialog-3871a6e9d538,它有效,但在另一种情况下。当您向下拖动对话框时,它将关闭它。如果您单击外部,它不会显示任何警报消息,也不会关闭对话框。

所以,我使用了来自/sf/answers/3551419651/的 @shijo 的好建议。

将这些行添加到onActivityCreated方法(或之后的任何其他生命周期方法onCreateView)。

@Override 
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    View touchOutsideView = getDialog().getWindow()
        .getDecorView()
        .findViewById(android.support.design.R.id.touch_outside);
    touchOutsideView.setOnClickListener(yourClickListener);
}
Run Code Online (Sandbox Code Playgroud)

就我而言,yourClickListener我检查文本并显示警报或关闭对话框:

private fun checkAndDismiss() {
    if (newText == oldText) {
        dismissAllowingStateLoss()
    } else {
        showDismissAlert()
    }
}
Run Code Online (Sandbox Code Playgroud)

当您创建 时BottomSheetDialogFragment,不要像/sf/answers/2987539201/setCancelable(false)中那样调用,否则这些方法可能不起作用。也许不要在styles 或.<item name="android:windowCloseOnTouchOutside">false</item>setCanceledOnTouchOutside(false)onCreateDialog


我还尝试了几种方法来覆盖取消行为,但没有成功。

<style name="BottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="android:windowCloseOnTouchOutside">false</item>
</style>

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

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    val dialog = super.onCreateDialog(savedInstanceState)

    dialog.setOnShowListener {
        val bottomSheet = dialog.findViewById<View>(
            android.support.design.R.id.design_bottom_sheet) as? FrameLayout
        val behavior = BottomSheetBehavior.from(bottomSheet)
        behavior.state = BottomSheetBehavior.STATE_EXPANDED
        behavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
            override fun onSlide(bottomSheet: View, slideOffset: Float) {
            }

            override fun onStateChanged(bottomSheet: View, newState: Int) {
                //showing the different states.
                when (newState) {
                    BottomSheetBehavior.STATE_HIDDEN -> dismiss() //if you want the modal to be dismissed when user drags the bottomsheet down
                    BottomSheetBehavior.STATE_EXPANDED -> {
                    }
                    BottomSheetBehavior.STATE_COLLAPSED -> {
                    }
                    BottomSheetBehavior.STATE_DRAGGING -> {
                    }
                    BottomSheetBehavior.STATE_SETTLING -> {
                    }
                }
            }
        })
        dialog.setOnCancelListener {
            // Doesn't matter what you write here, the dialog will be closed.
        }
        dialog.setOnDismissListener {
            // Doesn't matter what you write here, the dialog will be closed.
        }
    }

    return dialog
}

override fun onCancel(dialog: DialogInterface?) {
    // Doesn't matter what you write here, the dialog will be closed.
    super.onCancel(dialog)
}

override fun onDismiss(dialog: DialogInterface?) {
    // Doesn't matter what you write here, the dialog will be closed.
    super.onDismiss(dialog)
}
Run Code Online (Sandbox Code Playgroud)


sha*_*esh 5

bottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
   @Override
   public void onDismiss(DialogInterface dialog) {
       toast("dismissed");
   }
});
Run Code Online (Sandbox Code Playgroud)

  • 不鼓励仅使用代码的答案。请提供解释为什么以及您的答案如何解决问题。 (4认同)
  • 看来“BottomSheetDialogFragment”中没有这样的方法(“setOnDismissListener”)。 (2认同)