<i id='xtdtE'><tr id='xtdtE'><dt id='xtdtE'><q id='xtdtE'><span id='xtdtE'><b id='xtdtE'><form id='xtdtE'><ins id='xtdtE'></ins><ul id='xtdtE'></ul><sub id='xtdtE'></sub></form><legend id='xtdtE'></legend><bdo id='xtdtE'><pre id='xtdtE'><center id='xtdtE'></center></pre></bdo></b><th id='xtdtE'></th></span></q></dt></tr></i><div id='xtdtE'><tfoot id='xtdtE'></tfoot><dl id='xtdtE'><fieldset id='xtdtE'></fieldset></dl></div>

    <small id='xtdtE'></small><noframes id='xtdtE'>

  1. <legend id='xtdtE'><style id='xtdtE'><dir id='xtdtE'><q id='xtdtE'></q></dir></style></legend>
  2. <tfoot id='xtdtE'></tfoot>

      • <bdo id='xtdtE'></bdo><ul id='xtdtE'></ul>
    1. BottomSheetDialogFragment - 听用户事件解除

      时间:2023-08-30

            <bdo id='I1RAH'></bdo><ul id='I1RAH'></ul>

              <legend id='I1RAH'><style id='I1RAH'><dir id='I1RAH'><q id='I1RAH'></q></dir></style></legend>

              <small id='I1RAH'></small><noframes id='I1RAH'>

                <tbody id='I1RAH'></tbody>
            • <i id='I1RAH'><tr id='I1RAH'><dt id='I1RAH'><q id='I1RAH'><span id='I1RAH'><b id='I1RAH'><form id='I1RAH'><ins id='I1RAH'></ins><ul id='I1RAH'></ul><sub id='I1RAH'></sub></form><legend id='I1RAH'></legend><bdo id='I1RAH'><pre id='I1RAH'><center id='I1RAH'></center></pre></bdo></b><th id='I1RAH'></th></span></q></dt></tr></i><div id='I1RAH'><tfoot id='I1RAH'></tfoot><dl id='I1RAH'><fieldset id='I1RAH'></fieldset></dl></div>
              <tfoot id='I1RAH'></tfoot>
              • 本文介绍了BottomSheetDialogFragment - 听用户事件解除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                如何收听 BottomSheetDialogFragment 的最终解除?我只想在最终解雇时保存用户更改...

                How can I listen to a FINAL dismissal of a BottomSheetDialogFragment? I want to save user changes on the final dismissal only...

                我尝试了以下操作:

                方法一

                只有在通过向下滑动对话框(不是在后按或触摸外部)关闭对话框时才会触发

                This only fires, if the dialog is dismissed by swiping it down (not on back press or on touch outside)

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

                方法二

                这让我无法区分最终解雇和来自屏幕旋转或活动娱乐的解雇...

                This does not allow me to distinguish between a final dismissal and one that is coming from a screen rotation or activity recreation...

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

                问题

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

                How can I listen to an event that indicates, that the user has finished the dialog?

                推荐答案

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

                Although all similar questions on SO suggest using onDismiss I think following is the correct solution:

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

                如果发生以下情况,则会触发:

                This fires if:

                * the user presses back
                * the user presses outside of the dialog
                

                这不会触发:

                * on screen rotation and activity recreation
                

                解决方案

                结合 onCancelBottomSheetBehavior.BottomSheetCallback.onStateChanged,如下所示:

                Combine onCancel and BottomSheetBehavior.BottomSheetCallback.onStateChanged like following:

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

                这篇关于BottomSheetDialogFragment - 听用户事件解除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:android对话框活动位置 下一篇:在Android上将值从Dialog传回Activity的可靠方法?

                相关文章

                最新文章

                <i id='M50Tp'><tr id='M50Tp'><dt id='M50Tp'><q id='M50Tp'><span id='M50Tp'><b id='M50Tp'><form id='M50Tp'><ins id='M50Tp'></ins><ul id='M50Tp'></ul><sub id='M50Tp'></sub></form><legend id='M50Tp'></legend><bdo id='M50Tp'><pre id='M50Tp'><center id='M50Tp'></center></pre></bdo></b><th id='M50Tp'></th></span></q></dt></tr></i><div id='M50Tp'><tfoot id='M50Tp'></tfoot><dl id='M50Tp'><fieldset id='M50Tp'></fieldset></dl></div>

                  <legend id='M50Tp'><style id='M50Tp'><dir id='M50Tp'><q id='M50Tp'></q></dir></style></legend>
                      <bdo id='M50Tp'></bdo><ul id='M50Tp'></ul>
                  1. <small id='M50Tp'></small><noframes id='M50Tp'>

                  2. <tfoot id='M50Tp'></tfoot>