Skip to content

Commit a35b6b8

Browse files
dsn5ftimhappi
authored andcommitted
[Bottom Sheet] Add ability to disable dragging/expanding/collapsing the sheet when touching/scrolling the nested scrolling child view
PiperOrigin-RevId: 624204480
1 parent 9cf7bd3 commit a35b6b8

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

docs/components/BottomSheet.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ Behavior | Related method(s)
439439
`app:behavior_skipCollapsed` | `setSkipCollapsed`<br/>`getSkipCollapsed` | `false`
440440
`app:behavior_fitToContents` | `setFitToContents`<br/>`isFitToContents` | `true`
441441
`app:behavior_draggable` | `setDraggable`<br/>`isDraggable` | `true`
442+
`app:behavior_draggableOnNestedScroll` | `setDraggableOnNestedScroll`<br/>`isDraggableOnNestedScroll` | `true`
442443
`app:behavior_halfExpandedRatio` | `setHalfExpandedRatio`<br/>`getHalfExpandedRatio` | `0.5`
443444
`app:behavior_expandedOffset` | `setExpandedOffset`<br/>`getExpandedOffset` | `0dp`
444445
`app:behavior_significantVelocityThreshold` | `setSignificantVelocityThreshold` <br/> `getSignificantVelocityThreshold` | `500 pixels/s`

lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ void onLayout(@NonNull View bottomSheet) {}
306306

307307
private boolean draggable = true;
308308

309+
private boolean draggableOnNestedScroll = true;
310+
309311
@State int state = STATE_COLLAPSED;
310312

311313
@State int lastStableState = STATE_COLLAPSED;
@@ -398,6 +400,9 @@ public BottomSheetBehavior(@NonNull Context context, @Nullable AttributeSet attr
398400
setSkipCollapsed(
399401
a.getBoolean(R.styleable.BottomSheetBehavior_Layout_behavior_skipCollapsed, false));
400402
setDraggable(a.getBoolean(R.styleable.BottomSheetBehavior_Layout_behavior_draggable, true));
403+
setDraggableOnNestedScroll(
404+
a.getBoolean(
405+
R.styleable.BottomSheetBehavior_Layout_behavior_draggableOnNestedScroll, true));
401406
setSaveFlags(a.getInt(R.styleable.BottomSheetBehavior_Layout_behavior_saveFlags, SAVE_NONE));
402407
setHalfExpandedRatio(
403408
a.getFloat(R.styleable.BottomSheetBehavior_Layout_behavior_halfExpandedRatio, 0.5f));
@@ -739,6 +744,9 @@ public void onNestedPreScroll(
739744
if (isNestedScrollingCheckEnabled() && target != scrollingChild) {
740745
return;
741746
}
747+
if (!draggableOnNestedScroll && target == scrollingChild) {
748+
return;
749+
}
742750
int currentTop = child.getTop();
743751
int newTop = currentTop - dy;
744752
if (dy > 0) { // Upward
@@ -877,7 +885,7 @@ public boolean onNestedPreFling(
877885

878886
if (isNestedScrollingCheckEnabled() && nestedScrollingChildRef != null) {
879887
return target == nestedScrollingChildRef.get()
880-
&& (state != STATE_EXPANDED
888+
&& ((state != STATE_EXPANDED && draggableOnNestedScroll)
881889
|| super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY));
882890
} else {
883891
return false;
@@ -1170,7 +1178,7 @@ public boolean getSkipCollapsed() {
11701178
}
11711179

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

1195+
/**
1196+
* Sets whether this bottom sheet can be collapsed/expanded by dragging on the nested scrolling
1197+
* child view. Default is true.
1198+
*
1199+
* @param draggableOnNestedScroll {@code false} to prevent dragging the nested scrolling child
1200+
* view to collapse and expand the sheet
1201+
* @attr ref com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_draggableOnNestedScroll
1202+
*/
1203+
public void setDraggableOnNestedScroll(boolean draggableOnNestedScroll) {
1204+
this.draggableOnNestedScroll = draggableOnNestedScroll;
1205+
}
1206+
1207+
public boolean isDraggableOnNestedScroll() {
1208+
return draggableOnNestedScroll;
1209+
}
1210+
11871211
/*
11881212
* Sets the velocity threshold considered significant enough to trigger a slide
11891213
* to the next stable state.

lib/java/com/google/android/material/bottomsheet/res/values/attrs.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
means to expand and collapse the sheet. Attribute declaration is in
4242
the resources package. -->
4343
<attr name="behavior_draggable"/>
44+
<!-- Whether this bottom sheet is draggable when the nested scrolling child
45+
view is scrolled. Default is true. -->
46+
<attr name="behavior_draggableOnNestedScroll" format="boolean"/>
4447
<!-- The ratio to be used to set the height of half-expanded state in proportion to parent, when
4548
fitToContents is false. Defaults to true half, 0.5, if not explicitly set. Ratio must be a
4649
float value between 0 and 1 and produce a half-expanded state height larger than the

0 commit comments

Comments
 (0)