我正在尝试使用BottomSheetFragment实现BottomSheet,当用户在对话框外单击时,BottomSheetSFragment会折叠。我曾尝试覆盖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
上使用折叠/展开
将其添加到activityCreted getDialog().setCanceledOnTouchOutside(true)
中