Search code examples
androidandroid-support-libraryandroid-support-designbottom-sheet

Disable BottomSheet Drag


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?


Solution

  • 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();
            }
        }