我正在尝试使用BottomSheetFragment实现BottomSheet,当用户在对话框外单击时,该折叠会折叠。我尝试覆盖onCancel,但是将状态设置为STATE_COLLAPSED,但是它不起作用-当在外部单击时,BottomSheet消失了。也有setHideable(false)。因此,我希望当用户在外部单击时, Bottom Sheet 会崩溃,事实并非如此。我该如何实现?

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使用折叠/展开

最佳答案

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

关于android - 在BottomSheetFragment中将onCancel重写为setState为COLLAPSED,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60574613/

10-11 05:05