我有一个模态BottomSheetDialog
。对话框的当前和默认行为是,当我将对话框向下拖动一半时,它会消失。例如,假设对话框值从 0.0(折叠)到 1.0(展开)。所以当用户将它拖到 0.5 时,它就会崩溃。但是我想要的行为是当我将对话框拖到 0.8 并松开手指时关闭对话框。我怎样才能实现这种行为,有什么办法吗???
另外,我认为只有当我使用任何拖动按钮拖动它时才允许对话框关闭会很棒(大多数情况下它很简单ImageView
)。
所以我想要的是当用户向下移动对话框(拖动)时关闭对话框。
所以我目前的代码是:
public class FiltersBottomSheet extends BottomSheetDialogFragment {
private FragmentFilterBinding binding;
public FiltersBottomSheet() {}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_filter, container, false);
View view = binding.getRoot();
// I'm using data binding
// There is a little logic, but now you don't need it :)
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.getViewTreeObserver()
.addOnGlobalLayoutListener(() -> {
BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
LinearLayout bottomSheet = dialog.findViewById(R.id.bottom_frame);
if (bottomSheet != null) {
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
behavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
//Log.d("Tag___1", "NewState: " + newState);
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
//Log.d("Tag___1", "Offset: " + slideOffset);
}
});
}
});
}
}
所以首先我认为这onSlide()
可以帮助我,但是......这个方法只在用户展开或折叠对话框时调用。因此,例如,当用户触摸对话框并开始向下拖动时,不会调用 onSlide() 方法。你也看到上面我打电话view.getViewTreeObserver()
了,我试图添加onTouchListener
到这个视图中。问题就在这里,MotionEvenet.ACTION_UP
当用户在移动对话框后移开手指时没有调用。那么有什么想法吗?谢谢你。