From be136b9c5cedfb018b5a05fecac5ea9a3e242f36 Mon Sep 17 00:00:00 2001 From: Wilson Filho Date: Mon, 24 May 2021 17:59:55 -0300 Subject: [PATCH] feat: adds maxHeight to bottom sheet behavior --- .../bottomsheet/BottomSheetBehavior.java | 44 +++++++++++++++++++ .../material/bottomsheet/res/values/attrs.xml | 1 + 2 files changed, 45 insertions(+) diff --git a/lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java b/lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java index a84c549d029..267787d2fbb 100644 --- a/lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java +++ b/lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java @@ -197,6 +197,8 @@ public abstract static class BottomSheetCallback { private static final int NO_WIDTH = -1; + private static final int NO_HEIGHT = -1; + private boolean fitToContents = true; private boolean updateImportantForAccessibilityOnSiblings = false; @@ -222,6 +224,8 @@ public abstract static class BottomSheetCallback { private int maxWidth = NO_WIDTH; + private int maxHeight = NO_HEIGHT; + private int gestureInsetBottom; private boolean gestureInsetBottomIgnored; private boolean paddingBottomSystemWindowInsets; @@ -324,6 +328,12 @@ public BottomSheetBehavior(@NonNull Context context, @Nullable AttributeSet attr R.styleable.BottomSheetBehavior_Layout_android_maxWidth, NO_WIDTH)); } + if (a.hasValue(R.styleable.BottomSheetBehavior_Layout_android_maxHeight)) { + setMaxHeight( + a.getDimensionPixelSize( + R.styleable.BottomSheetBehavior_Layout_android_maxHeight, NO_HEIGHT)); + } + TypedValue value = a.peekValue(R.styleable.BottomSheetBehavior_Layout_behavior_peekHeight); if (value != null && value.data == PEEK_HEIGHT_AUTO) { setPeekHeight(value.data); @@ -453,6 +463,18 @@ public void run() { } }); } + + int height = child.getMeasuredHeight(); + if (height > maxHeight && maxHeight != NO_HEIGHT) { + final ViewGroup.LayoutParams lp = child.getLayoutParams(); + lp.height = maxHeight; + child.post(new Runnable() { + @Override + public void run() { + child.setLayoutParams(lp); + } + }); + } } if (viewDragHelper == null) { viewDragHelper = ViewDragHelper.create(parent, dragCallback); @@ -829,6 +851,28 @@ public int getMaxWidth() { return maxWidth; } + /** + * Sets the maximum height of the bottom sheet. + * This method should be called before {@link BottomSheetDialog#show()} in order for the height to + * be adjusted as expected. + * + * @param maxHeight The maximum height in pixels to be set + * @attr ref com.google.android.material.R.styleable#BottomSheetBehavior_Layout_android_maxHeight + * @see #getMaxHeight() + */ + public void setMaxHeight(@Px int maxHeight) { + this.maxHeight = maxHeight; + } + + /** + * Returns the bottom sheet's maximum height, or -1 if no maximum height is set. + * + * @attr ref com.google.android.material.R.styleable#BottomSheetBehavior_Layout_android_maxHeight + * @see #setMaxHeight(int) + */ + @Px + public int getMaxHeight() { return maxHeight; } + /** * Sets the height of the bottom sheet when it is collapsed. * diff --git a/lib/java/com/google/android/material/bottomsheet/res/values/attrs.xml b/lib/java/com/google/android/material/bottomsheet/res/values/attrs.xml index 9652afe6645..729f0e5ba9e 100644 --- a/lib/java/com/google/android/material/bottomsheet/res/values/attrs.xml +++ b/lib/java/com/google/android/material/bottomsheet/res/values/attrs.xml @@ -83,6 +83,7 @@ +