Skip to content

Commit

Permalink
[Bottom Sheet] Allow dragging the sheet on overscroll when draggableO…
Browse files Browse the repository at this point in the history
…nNestedScroll=false

PiperOrigin-RevId: 624269903
  • Loading branch information
dsn5ft authored and imhappi committed Apr 15, 2024
1 parent a35b6b8 commit d056cc3
Showing 1 changed file with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ void onLayout(@NonNull View bottomSheet) {}
private boolean draggable = true;

private boolean draggableOnNestedScroll = true;
private boolean draggableOnNestedScrollLastDragIgnored;

@State int state = STATE_COLLAPSED;

Expand Down Expand Up @@ -744,12 +745,14 @@ 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
if (dy > 0) { // Upward swipe
if (!draggableOnNestedScroll && target == scrollingChild && target.canScrollVertically(1)) {
// Prevent dragging if draggableOnNestedScroll=false and we can scroll the scrolling child.
draggableOnNestedScrollLastDragIgnored = true;
return;
}
if (newTop < getExpandedOffset()) {
consumed[1] = currentTop - getExpandedOffset();
ViewCompat.offsetTopAndBottom(child, -consumed[1]);
Expand All @@ -764,8 +767,14 @@ public void onNestedPreScroll(
ViewCompat.offsetTopAndBottom(child, -dy);
setStateInternal(STATE_DRAGGING);
}
} else if (dy < 0) { // Downward
if (!target.canScrollVertically(-1)) {
} else if (dy < 0) { // Downward swipe
boolean canScrollUp = target.canScrollVertically(-1);
if (!draggableOnNestedScroll && target == scrollingChild && canScrollUp) {
// Prevent dragging if draggableOnNestedScroll=false and we can scroll the scrolling child.
draggableOnNestedScrollLastDragIgnored = true;
return;
}
if (!canScrollUp) {
if (newTop <= collapsedOffset || canBeHiddenByDragging()) {
if (!draggable) {
// Prevent dragging
Expand All @@ -785,6 +794,7 @@ public void onNestedPreScroll(
dispatchOnSlide(child.getTop());
lastNestedScrollDy = dy;
nestedScrolled = true;
draggableOnNestedScrollLastDragIgnored = false;
}

@Override
Expand Down Expand Up @@ -885,7 +895,7 @@ public boolean onNestedPreFling(

if (isNestedScrollingCheckEnabled() && nestedScrollingChildRef != null) {
return target == nestedScrollingChildRef.get()
&& ((state != STATE_EXPANDED && draggableOnNestedScroll)
&& ((state != STATE_EXPANDED && !draggableOnNestedScrollLastDragIgnored)
|| super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY));
} else {
return false;
Expand Down

0 comments on commit d056cc3

Please sign in to comment.