android–BottomSheetDialogFragment– 听取用户事件解雇

android–BottomSheetDialogFragment– 听取用户事件解雇,第1张

概述我如何听取最终解雇BottomSheetDialogFragment?我想在最终解雇时保存用户更改…我试过以下:方法1如果通过向下滑动对话框解除对话(不在背面按压或外面触摸),则仅触发此 *** 作@OverridepublicDialogonCreateDialog(BundlesavedInstanceState){Dialogd=super.onCreateD

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

我试过以下:

方法1

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

@OverrIDepublic 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;}

方法2

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

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

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

解决方法:

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

@OverrIDepublic voID onCancel(DialogInterface dialog){    super.onCancel(dialog);    Toast.makeText(MainApp.get(), "CANCEL", Toast.LENGTH_SHORT).show();}

如果:

* the user presses back* the user presses outsIDe of the dialog

这不会引发:

* on screen rotation and activity recreation

结合onCancel和BottomSheetBehavior.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();    }}
总结

以上是内存溢出为你收集整理的android – BottomSheetDialogFragment – 听取用户事件解雇全部内容,希望文章能够帮你解决android – BottomSheetDialogFragment – 听取用户事件解雇所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1096966.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存