8282 */
8383public class BottomSheetBehavior <V extends View > extends CoordinatorLayout .Behavior <V > {
8484
85-
8685 /** Callback for monitoring events about bottom sheets. */
8786 public abstract static class BottomSheetCallback {
8887
@@ -197,6 +196,8 @@ public abstract static class BottomSheetCallback {
197196
198197 private static final int NO_WIDTH = -1 ;
199198
199+ private static final int NO_HEIGHT = -1 ;
200+
200201 private boolean fitToContents = true ;
201202
202203 private boolean updateImportantForAccessibilityOnSiblings = false ;
@@ -222,6 +223,8 @@ public abstract static class BottomSheetCallback {
222223
223224 private int maxWidth = NO_WIDTH ;
224225
226+ private int maxHeight = NO_HEIGHT ;
227+
225228 private int gestureInsetBottom ;
226229 private boolean gestureInsetBottomIgnored ;
227230 private boolean paddingBottomSystemWindowInsets ;
@@ -326,6 +329,12 @@ public BottomSheetBehavior(@NonNull Context context, @Nullable AttributeSet attr
326329 R .styleable .BottomSheetBehavior_Layout_android_maxWidth , NO_WIDTH ));
327330 }
328331
332+ if (a .hasValue (R .styleable .BottomSheetBehavior_Layout_android_maxHeight )) {
333+ setMaxHeight (
334+ a .getDimensionPixelSize (
335+ R .styleable .BottomSheetBehavior_Layout_android_maxHeight , NO_HEIGHT ));
336+ }
337+
329338 TypedValue value = a .peekValue (R .styleable .BottomSheetBehavior_Layout_behavior_peekHeight );
330339 if (value != null && value .data == PEEK_HEIGHT_AUTO ) {
331340 setPeekHeight (value .data );
@@ -445,18 +454,7 @@ public boolean onLayoutChild(
445454 ViewCompat .setImportantForAccessibility (child , ViewCompat .IMPORTANT_FOR_ACCESSIBILITY_YES );
446455 }
447456
448- // Adjust the width to be at most the maxWidth if needed.
449- int width = child .getMeasuredWidth ();
450- if (width > maxWidth && maxWidth != NO_WIDTH ) {
451- final ViewGroup .LayoutParams lp = child .getLayoutParams ();
452- lp .width = maxWidth ;
453- child .post (new Runnable () {
454- @ Override
455- public void run () {
456- child .setLayoutParams (lp );
457- }
458- });
459- }
457+ adjustChildWidthAndHeightIfNeeded (child );
460458 }
461459 if (viewDragHelper == null ) {
462460 viewDragHelper = ViewDragHelper .create (parent , dragCallback );
@@ -840,6 +838,29 @@ public int getMaxWidth() {
840838 return maxWidth ;
841839 }
842840
841+ /**
842+ * Sets the maximum height of the bottom sheet. This method should be called before {@link
843+ * BottomSheetDialog#show()} in order for the height to be adjusted as expected.
844+ *
845+ * @param maxHeight The maximum height in pixels to be set
846+ * @attr ref com.google.android.material.R.styleable#BottomSheetBehavior_Layout_android_maxHeight
847+ * @see #getMaxHeight()
848+ */
849+ public void setMaxHeight (@ Px int maxHeight ) {
850+ this .maxHeight = maxHeight ;
851+ }
852+
853+ /**
854+ * Returns the bottom sheet's maximum height, or -1 if no maximum height is set.
855+ *
856+ * @attr ref com.google.android.material.R.styleable#BottomSheetBehavior_Layout_android_maxHeight
857+ * @see #setMaxHeight(int)
858+ */
859+ @ Px
860+ public int getMaxHeight () {
861+ return maxHeight ;
862+ }
863+
843864 /**
844865 * Sets the height of the bottom sheet when it is collapsed.
845866 *
@@ -1215,6 +1236,33 @@ void setStateInternal(@State int state) {
12151236 updateAccessibilityActions ();
12161237 }
12171238
1239+ private void adjustChildWidthAndHeightIfNeeded (@ NonNull final V child ) {
1240+ final ViewGroup .LayoutParams lp = child .getLayoutParams ();
1241+ boolean layoutHasChanges = false ;
1242+
1243+ int width = child .getMeasuredWidth ();
1244+ if (width > maxWidth && maxWidth != NO_WIDTH ) {
1245+ lp .width = maxWidth ;
1246+ layoutHasChanges = true ;
1247+ }
1248+
1249+ int height = child .getMeasuredHeight ();
1250+ if (height > maxHeight && maxHeight != NO_HEIGHT ) {
1251+ lp .height = maxHeight ;
1252+ layoutHasChanges = true ;
1253+ }
1254+
1255+ if (layoutHasChanges ) {
1256+ child .post (
1257+ new Runnable () {
1258+ @ Override
1259+ public void run () {
1260+ child .setLayoutParams (lp );
1261+ }
1262+ });
1263+ }
1264+ }
1265+
12181266 private void updateDrawableForTargetState (@ State int state ) {
12191267 if (state == STATE_SETTLING ) {
12201268 // Special case: we want to know which state we're settling to, so wait for another call.
0 commit comments