2

我正在尝试使用 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

4

1 回答 1

1

在activityCreted中添加这个getDialog().setCanceledOnTouchOutside(true)

于 2020-03-07T05:36:52.850 回答