Skip to content

Commit

Permalink
[Bottom Sheet] Add ability to disable dragging/expanding/collapsing t…
Browse files Browse the repository at this point in the history
…he sheet when touching/scrolling the nested scrolling child view

PiperOrigin-RevId: 624204480
  • Loading branch information
dsn5ft authored and imhappi committed Apr 12, 2024
1 parent 9cf7bd3 commit a35b6b8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/components/BottomSheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ Behavior | Related method(s)
`app:behavior_skipCollapsed` | `setSkipCollapsed`<br/>`getSkipCollapsed` | `false`
`app:behavior_fitToContents` | `setFitToContents`<br/>`isFitToContents` | `true`
`app:behavior_draggable` | `setDraggable`<br/>`isDraggable` | `true`
`app:behavior_draggableOnNestedScroll` | `setDraggableOnNestedScroll`<br/>`isDraggableOnNestedScroll` | `true`
`app:behavior_halfExpandedRatio` | `setHalfExpandedRatio`<br/>`getHalfExpandedRatio` | `0.5`
`app:behavior_expandedOffset` | `setExpandedOffset`<br/>`getExpandedOffset` | `0dp`
`app:behavior_significantVelocityThreshold` | `setSignificantVelocityThreshold` <br/> `getSignificantVelocityThreshold` | `500 pixels/s`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ void onLayout(@NonNull View bottomSheet) {}

private boolean draggable = true;

private boolean draggableOnNestedScroll = true;

@State int state = STATE_COLLAPSED;

@State int lastStableState = STATE_COLLAPSED;
Expand Down Expand Up @@ -398,6 +400,9 @@ public BottomSheetBehavior(@NonNull Context context, @Nullable AttributeSet attr
setSkipCollapsed(
a.getBoolean(R.styleable.BottomSheetBehavior_Layout_behavior_skipCollapsed, false));
setDraggable(a.getBoolean(R.styleable.BottomSheetBehavior_Layout_behavior_draggable, true));
setDraggableOnNestedScroll(
a.getBoolean(
R.styleable.BottomSheetBehavior_Layout_behavior_draggableOnNestedScroll, true));
setSaveFlags(a.getInt(R.styleable.BottomSheetBehavior_Layout_behavior_saveFlags, SAVE_NONE));
setHalfExpandedRatio(
a.getFloat(R.styleable.BottomSheetBehavior_Layout_behavior_halfExpandedRatio, 0.5f));
Expand Down Expand Up @@ -739,6 +744,9 @@ public void onNestedPreScroll(
if (isNestedScrollingCheckEnabled() && target != scrollingChild) {
return;
}
if (!draggableOnNestedScroll && target == scrollingChild) {
return;
}
int currentTop = child.getTop();
int newTop = currentTop - dy;
if (dy > 0) { // Upward
Expand Down Expand Up @@ -877,7 +885,7 @@ public boolean onNestedPreFling(

if (isNestedScrollingCheckEnabled() && nestedScrollingChildRef != null) {
return target == nestedScrollingChildRef.get()
&& (state != STATE_EXPANDED
&& ((state != STATE_EXPANDED && draggableOnNestedScroll)
|| super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY));
} else {
return false;
Expand Down Expand Up @@ -1170,7 +1178,7 @@ public boolean getSkipCollapsed() {
}

/**
* Sets whether this bottom sheet is can be collapsed/expanded by dragging. Note: When disabling
* Sets whether this bottom sheet can be collapsed/expanded by dragging. Note: When disabling
* dragging, an app will require to implement a custom way to expand/collapse the bottom sheet
*
* @param draggable {@code false} to prevent dragging the sheet to collapse and expand
Expand All @@ -1184,6 +1192,22 @@ public boolean isDraggable() {
return draggable;
}

/**
* Sets whether this bottom sheet can be collapsed/expanded by dragging on the nested scrolling
* child view. Default is true.
*
* @param draggableOnNestedScroll {@code false} to prevent dragging the nested scrolling child
* view to collapse and expand the sheet
* @attr ref com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_draggableOnNestedScroll
*/
public void setDraggableOnNestedScroll(boolean draggableOnNestedScroll) {
this.draggableOnNestedScroll = draggableOnNestedScroll;
}

public boolean isDraggableOnNestedScroll() {
return draggableOnNestedScroll;
}

/*
* Sets the velocity threshold considered significant enough to trigger a slide
* to the next stable state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
means to expand and collapse the sheet. Attribute declaration is in
the resources package. -->
<attr name="behavior_draggable"/>
<!-- Whether this bottom sheet is draggable when the nested scrolling child
view is scrolled. Default is true. -->
<attr name="behavior_draggableOnNestedScroll" format="boolean"/>
<!-- The ratio to be used to set the height of half-expanded state in proportion to parent, when
fitToContents is false. Defaults to true half, 0.5, if not explicitly set. Ratio must be a
float value between 0 and 1 and produce a half-expanded state height larger than the
Expand Down

0 comments on commit a35b6b8

Please sign in to comment.