Skip to content

Commit bb0bfe1

Browse files
committed
[BottomSheet] Fixed issue where peekHeight is more than the height of the contents
PiperOrigin-RevId: 329722448 (cherry picked from commit e944d1b)
1 parent ec7f7cb commit bb0bfe1

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.google.android.material.bottomsheet;
1818

1919
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
20+
import static java.lang.Math.max;
21+
import static java.lang.Math.min;
2022

2123
import android.animation.ValueAnimator;
2224
import android.animation.ValueAnimator.AnimatorUpdateListener;
@@ -76,6 +78,7 @@
7678
*/
7779
public class BottomSheetBehavior<V extends View> extends CoordinatorLayout.Behavior<V> {
7880

81+
7982
/** Callback for monitoring events about bottom sheets. */
8083
public abstract static class BottomSheetCallback {
8184

@@ -253,6 +256,7 @@ public abstract static class BottomSheetCallback {
253256

254257
private boolean nestedScrolled;
255258

259+
private int childHeight;
256260
int parentWidth;
257261
int parentHeight;
258262

@@ -411,7 +415,8 @@ public boolean onLayoutChild(
411415
// Offset the bottom sheet
412416
parentWidth = parent.getWidth();
413417
parentHeight = parent.getHeight();
414-
fitToContentsOffset = Math.max(0, parentHeight - child.getHeight());
418+
childHeight = child.getHeight();
419+
fitToContentsOffset = max(0, parentHeight - childHeight);
415420
calculateHalfExpandedOffset();
416421
calculateCollapsedOffset();
417422

@@ -772,7 +777,7 @@ public final void setPeekHeight(int peekHeight, boolean animate) {
772777
}
773778
} else if (peekHeightAuto || this.peekHeight != peekHeight) {
774779
peekHeightAuto = false;
775-
this.peekHeight = Math.max(0, peekHeight);
780+
this.peekHeight = max(0, peekHeight);
776781
layout = true;
777782
}
778783
// If sheet is already laid out, recalculate the collapsed offset based on new setting.
@@ -1130,10 +1135,11 @@ private void updateDrawableForTargetState(@State int state) {
11301135

11311136
private int calculatePeekHeight() {
11321137
if (peekHeightAuto) {
1133-
return Math.max(peekHeightMin, parentHeight - parentWidth * 9 / 16);
1138+
int desiredHeight = max(peekHeightMin, parentHeight - parentWidth * 9 / 16);
1139+
return min(desiredHeight, childHeight);
11341140
}
11351141
if (!gestureInsetBottomIgnored && gestureInsetBottom > 0) {
1136-
return Math.max(peekHeight, gestureInsetBottom + peekHeightGestureInsetBuffer);
1142+
return max(peekHeight, gestureInsetBottom + peekHeightGestureInsetBuffer);
11371143
}
11381144
return peekHeight;
11391145
}
@@ -1142,7 +1148,7 @@ private void calculateCollapsedOffset() {
11421148
int peek = calculatePeekHeight();
11431149

11441150
if (fitToContents) {
1145-
collapsedOffset = Math.max(parentHeight - peek, fitToContentsOffset);
1151+
collapsedOffset = max(parentHeight - peek, fitToContentsOffset);
11461152
} else {
11471153
collapsedOffset = parentHeight - peek;
11481154
}

0 commit comments

Comments
 (0)