5

Due to my low reputation point, I can't comment. So i'm extending this question: Disabling User dragging on BottomSheet

The solution provided by Ray W works but now it expands by sliding and dragging on parent view (CoordinatorLayout).

Image

In that image, if I drag on "Unwanted Drag Area", BottomSheet slides up. How can I filtered out or stop the touch events on unwanted view?

Community
  • 1
  • 1
Fahim
  • 59
  • 1
  • 1
  • 6

4 Answers4

7

For the Kotlin, simply add below line in your code,

behaviour.isDraggable = false
Jay makwana
  • 97
  • 1
  • 4
0

Change the onInterceptTouchEvent function return value from Ray W solution to this:

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
  return super.onInterceptTouchEvent(parent, child, event) && mAllowUserDragging;
}

One more thing, if you have a ListView in BottomSheetLayout, then scrolling items in ListView will change the bottomSheetBehavior state to "STATE_DRAGGING" from "STATE_EXPANDED". Sample code piece:

@Override
    public void onBackPressed() {    
        if(isBottomViewOpen){ // set this bool in behavior callback
            behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        }else {
            super.onBackPressed();
        }
    }
Community
  • 1
  • 1
Fahim
  • 59
  • 1
  • 1
  • 6
  • in regards to your listview comment. What's the fix for this? I have a recyclerview in a bottomsheet and I'm experiencing the same issue. If I try to drag the bottomsheet anywhere else other than in the recyclerview, the bottomsheet remains expanded as expected. Only when dragging the recyclerview does the bottomsheet start dragging as well. – Zac Jul 22 '16 at 17:32
  • I ended up using this one: https://github.com/umano/AndroidSlidingUpPanel There are lots of other problems in Bottomsheet e.g. not refreshing ListView in BottomSheet. So I quit and use SlidingUpPanel instead. – Fahim Jul 24 '16 at 04:58
0

Simple solution, It solved two problem I had.

1- It block the dragging event.

2- It solves the listView scrolling problem

CoordinatorLayout.Behavior<View> behavior;

View profile_config_layout_bottom_sheet = findViewById(R.id.list_view_audience_profile_config_layout);

CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) profile_config_layout_bottom_sheet.getLayoutParams();
behavior = layoutParams.getBehavior();
assert behavior != null;
((BottomSheetBehavior<View>) behavior).addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        if (newState == BottomSheetBehavior.STATE_DRAGGING) {
            ((BottomSheetBehavior<View>) behavior).setState(BottomSheetBehavior.STATE_EXPANDED);
        } 
    }
    @Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {}
});

Show the dialog with:

((BottomSheetBehavior) behavior).setState(BottomSheetBehavior.STATE_EXPANDED);

ankalagba
  • 94
  • 1
  • 8
  • Please [edit] your answer to explain what this code does and how it answers the question, so that it is useful to the OP as well as other users with similar issues. Code-only answers are discouraged on Stack Overflow because they don't explain how it solves the problem. – FluffyKitten Oct 08 '20 at 02:00
-3

Look at this :

mBottomSheetBehavior1.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_DRAGGING) {
                    mBottomSheetBehavior1.setState(BottomSheetBehavior.STATE_EXPANDED);
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                Log.i("BottomSheetCallback", "slideOffset: " + slideOffset);
            }
        });

Close BottomSheet when onBackPress :

@Override
    public void onBackPressed() {
        if(mBottomSheetBehavior1.getState() != 4) {
            mBottomSheetBehavior1.setState(BottomSheetBehavior.STATE_COLLAPSED);
        }
        else {
            super.onBackPressed();
        }
    }
SANAT
  • 8,489
  • 55
  • 66