【问题标题】:Override onCancel in BottomSheetFragment to setState to COLLAPSED覆盖 BottomSheetFragment 中的 onCancel 以将 setState 设置为 COLLAPSED
【发布时间】:2020-03-07 05:14:40
【问题描述】:

我正在尝试使用 BottomSheetFragment 实现一个 BottomSheet,当用户在对话框外单击时它会折叠。我尝试覆盖onCancel,但将状态设置为STATE_COLLAPSED,但它不起作用 - 在外部单击时,BottomSheet 消失。还有setHideable(false)。因此,我希望当用户在外部单击时底页会折叠,但事实并非如此。我怎样才能做到这一点?

public class MyBottomSheet extends BottomSheetDialogFragment {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.eazyotp_auto_capture_bottomsheet, container, false);
    }

    @Override
    public void onCancel(@NonNull DialogInterface dialog) {
        super.onCancel(dialog);
        behavior.setState(BottomSheetBehavior.STATE_COLLAPSED); // does not work

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        behavior = getDialog().getBehavior();
        behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        behavior.setHideable(false);
        behavior.setPeekHeight(70); 
       // following works well - even when user drags the bottomsheet it gets into collapsed state.
        imageView.setOnClickListener(v -> {
        if(behavior.getState() == BottomSheetBehavior.STATE_EXPANDED)
            behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        else
            behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    });

    }
}

当我做setCancelable(false) 时,我不能在imageView 上使用折叠/展开

【问题讨论】:

  • 所以你想在用户点击外部时消失对话框? this 是你的答案吗?

标签: android bottom-sheet android-bottomsheetdialog


【解决方案1】:

将此添加到activityCreted getDialog().setCanceledOnTouchOutside(true)

【讨论】:

  • 谢谢。但这不会折叠 BottomSheet,它会保持展开状态。
  • 对不起我的错误,让它成为现实
  • 这不会像现在一样正常触发 onCancel 吗?它不会折叠工作表。
  • 将其设为 false 和 setCancalable(false)
猜你喜欢
  • 2021-05-31
  • 1970-01-01
  • 2020-06-04
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
  • 1970-01-01
  • 2020-03-03
  • 1970-01-01
相关资源
最近更新 更多