6

有什么方法可以捕获 BottomSheetDialogFragment 的解雇/取消?

底片类

public class ContactDetailFragment extends BottomSheetDialogFragment
{
    private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback()
    {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState)
        {
            if (newState == BottomSheetBehavior.STATE_HIDDEN)
            {
                dismiss();
            }
        }

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

    @Override
    public void setupDialog(Dialog dialog, int style)
    {
        super.setupDialog(dialog, style);
        View contentView = View.inflate(getContext(), R.layout.fragment_contactdetail, null);

        dialog.setContentView(contentView);

        BottomSheetBehavior mBottomSheetBehavior = BottomSheetBehavior.from(((View) contentView.getParent()));
        if (mBottomSheetBehavior != null)
        {
            mBottomSheetBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback);
            mBottomSheetBehavior.setPeekHeight((int) DisplayUtils.dpToPixels(CONTACT_DETAIL_PEEK_HEIGHT, getResources().getDisplayMetrics()));
        }
    }
}

我尝试过的不起作用

  • setupDialog添加dialog.setOnCancelListener();dialog.setOnDismissListener();永远不会被触发
  • 仅当用户向下拖动底部表通过折叠状态时才会触发底部表行为onStateChanged,并且没有解除/取消状态
  • 将相同的 oncancel/ondismiss 侦听器添加到 BottomSheetDialogFragment 的实例化中,使用ContactDetailFragment.getDialog().setOnCancelListener()不会被触发

鉴于它本质上是一个对话片段,必须有某种方法来捕捉解雇?

4

1 回答 1

13

找到了一个简单的解决方案。在 BottomSheetDialogFragment 中使用onDestroyoronDetach可以让我正确解雇

于 2016-03-28T06:05:58.413 回答