Skip to content

Commit

Permalink
[BottomSheet] Fixed issue where peekHeight is more than the height of…
Browse files Browse the repository at this point in the history
… the contents

PiperOrigin-RevId: 329722448
(cherry picked from commit e944d1b)
  • Loading branch information
ymarian committed Sep 2, 2020
1 parent ec7f7cb commit bb0bfe1
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,6 +78,7 @@
*/
public class BottomSheetBehavior<V extends View> extends CoordinatorLayout.Behavior<V> {


/** Callback for monitoring events about bottom sheets. */
public abstract static class BottomSheetCallback {

Expand Down Expand Up @@ -253,6 +256,7 @@ public abstract static class BottomSheetCallback {

private boolean nestedScrolled;

private int childHeight;
int parentWidth;
int parentHeight;

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit bb0bfe1

Please sign in to comment.