Reputation: 3245
I have a modal BottomSheetDialog
. Current and the default behavior of the dialog is, when I drag the dialog down by half, it dismisses. E.g assume the dialog value is from 0.0 (Collapsed) to 1.0 (Expanded). So when user drags it down to 0.5, it collapses. But the behavior that I want, is to dismiss the dialog when I drag the dialog down to 0.8 and take off my finger. How can I achieve this behavior, is there any way???
Also, I think it would be great to allow the dialog dismiss only when I drag it with any drag button (most of cases it's a simple ImageView
).
So what I want is to dismiss the dialog, when user moves the dialog(drags) a little bit down.
So my current code is:
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);
}
});
}
});
}
}
So first of all I thought that onSlide()
can help me, but... This method called only when user expands or collapses the dialog. So e.g. when user touches the dialog and starts to drag it down, the onSlide() method doesn't called. Also you see above I called view.getViewTreeObserver()
, I've tried to add onTouchListener
to this view. And the problem is here, that the MotionEvenet.ACTION_UP
is not calling when the user removes his finger after moving the dialog. So any idea? Thank you.
Upvotes: 4
Views: 6774
Reputation: 668
I think you can customize the attribute behavior_halfExpandedRatio
and you can also customize the callbacks when sliding:
val bottomSheetCallback = object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
// Do something for new state
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {
// Do something for slide offset
}
}
bottomSheetBehavior.addBottomSheetCallback(bottomSheetCallback)
Upvotes: 2
Reputation: 459
If you want to modify the bottom sheet dialog behaviour in your bottom dialog fragment use this:
@Override
public void onViewCreated(NotNull@ View view, Nullable@ Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState)
view.getViewTreeObserver()
.addOnGlobalLayoutListener(() -> {
BottomSheetDialog dialog =(BottomSheetDialog) getDialog ()
if (dialog != null) {
FrameLayout bottomSheet = dialog.findViewById (R.id.design_bottom_sheet)
if (bottomSheet != null) {
BottomSheetBehavior behavior = BottomSheetBehavior.from (bottomSheet)
behavior.setState(BottomSheetBehavior.STATE_EXPANDED)
behavior.setSkipCollapsed(true)
behavior.setHideable(false)
}
}
})
}
This will help you to state expanded and hideable false and you can put custom size to be expanded also.
Upvotes: 4