From bb0bfe183589e9ea47bec0a5086860c40f697fd4 Mon Sep 17 00:00:00 2001 From: marianomartin Date: Wed, 2 Sep 2020 11:59:19 -0400 Subject: [PATCH] [BottomSheet] Fixed issue where peekHeight is more than the height of the contents PiperOrigin-RevId: 329722448 (cherry picked from commit e944d1b2a6ee5d9d5a338de0c0061f7b02790f77) --- .../bottomsheet/BottomSheetBehavior.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java b/lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java index 6463463b370..d95b75e5fa6 100644 --- a/lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java +++ b/lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java @@ -17,6 +17,8 @@ package com.google.android.material.bottomsheet; import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP; +import static java.lang.Math.max; +import static java.lang.Math.min; import android.animation.ValueAnimator; import android.animation.ValueAnimator.AnimatorUpdateListener; @@ -76,6 +78,7 @@ */ public class BottomSheetBehavior extends CoordinatorLayout.Behavior { + /** Callback for monitoring events about bottom sheets. */ public abstract static class BottomSheetCallback { @@ -253,6 +256,7 @@ public abstract static class BottomSheetCallback { private boolean nestedScrolled; + private int childHeight; int parentWidth; int parentHeight; @@ -411,7 +415,8 @@ public boolean onLayoutChild( // Offset the bottom sheet parentWidth = parent.getWidth(); parentHeight = parent.getHeight(); - fitToContentsOffset = Math.max(0, parentHeight - child.getHeight()); + childHeight = child.getHeight(); + fitToContentsOffset = max(0, parentHeight - childHeight); calculateHalfExpandedOffset(); calculateCollapsedOffset(); @@ -772,7 +777,7 @@ public final void setPeekHeight(int peekHeight, boolean animate) { } } else if (peekHeightAuto || this.peekHeight != peekHeight) { peekHeightAuto = false; - this.peekHeight = Math.max(0, peekHeight); + this.peekHeight = max(0, peekHeight); layout = true; } // If sheet is already laid out, recalculate the collapsed offset based on new setting. @@ -1130,10 +1135,11 @@ private void updateDrawableForTargetState(@State int state) { private int calculatePeekHeight() { if (peekHeightAuto) { - return Math.max(peekHeightMin, parentHeight - parentWidth * 9 / 16); + int desiredHeight = max(peekHeightMin, parentHeight - parentWidth * 9 / 16); + return min(desiredHeight, childHeight); } if (!gestureInsetBottomIgnored && gestureInsetBottom > 0) { - return Math.max(peekHeight, gestureInsetBottom + peekHeightGestureInsetBuffer); + return max(peekHeight, gestureInsetBottom + peekHeightGestureInsetBuffer); } return peekHeight; } @@ -1142,7 +1148,7 @@ private void calculateCollapsedOffset() { int peek = calculatePeekHeight(); if (fitToContents) { - collapsedOffset = Math.max(parentHeight - peek, fitToContentsOffset); + collapsedOffset = max(parentHeight - peek, fitToContentsOffset); } else { collapsedOffset = parentHeight - peek; }