Catch解雇BottomSheetDialogFragment

时间:2016-03-28 05:53:15

标签: android android-dialogfragment bottom-sheet

有没有办法解决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()不会被触发

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

1 个答案:

答案 0 :(得分:8)

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