diff --git a/lib/java/com/google/android/material/carousel/CarouselLayoutManager.java b/lib/java/com/google/android/material/carousel/CarouselLayoutManager.java index aa8f8f00693..b64d042fa53 100644 --- a/lib/java/com/google/android/material/carousel/CarouselLayoutManager.java +++ b/lib/java/com/google/android/material/carousel/CarouselLayoutManager.java @@ -307,6 +307,11 @@ public void onLayoutChildren(Recycler recycler, State state) { lastItemCount = getItemCount(); } + @Override + public boolean isAutoMeasureEnabled() { + return true; + } + private void recalculateKeylineStateList(Recycler recycler) { View firstChild = recycler.getViewForPosition(0); measureChildWithMargins(firstChild, 0, 0); diff --git a/lib/java/com/google/android/material/carousel/CarouselOrientationHelper.java b/lib/java/com/google/android/material/carousel/CarouselOrientationHelper.java index d9b96ab24b7..80420d934a6 100644 --- a/lib/java/com/google/android/material/carousel/CarouselOrientationHelper.java +++ b/lib/java/com/google/android/material/carousel/CarouselOrientationHelper.java @@ -83,6 +83,16 @@ static CarouselOrientationHelper createOrientationHelper( /** Returns the y-coordinate of the bottom edge of the parent recycler view. */ abstract int getParentBottom(); + + /** + * Returns the space occupied by this View in the cross (non-scrolling) axis including + * decorations and margins. + * + * @param child The view element to check + * @return total space occupied by this view in the perpendicular orientation to current one + */ + abstract int getDecoratedCrossAxisMeasurement(View child); + /** * Helper method that calls {@link CarouselLayoutManager#layoutDecoratedWithMargins(View, int, * int, int, int)} with the correct coordinates according to the orientation. @@ -182,13 +192,23 @@ int getParentBottom() { return carouselLayoutManager.getHeight(); } + @Override + int getDecoratedCrossAxisMeasurement(View child) { + final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) + child.getLayoutParams(); + return carouselLayoutManager.getDecoratedMeasuredWidth(child) + params.leftMargin + + params.rightMargin; + } + @Override public void layoutDecoratedWithMargins(View child, int head, int tail) { + int left = getParentLeft(); + int right = left + getDecoratedCrossAxisMeasurement(child); carouselLayoutManager.layoutDecoratedWithMargins( child, - /* left= */ getParentLeft(), + /* left= */ left, /* top= */ head, - /* right= */ getParentRight(), + /* right= */ right, /* bottom= */ tail); } @@ -276,14 +296,24 @@ int getParentBottom() { return carouselLayoutManager.getHeight() - carouselLayoutManager.getPaddingBottom(); } + @Override + int getDecoratedCrossAxisMeasurement(View child) { + final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) + child.getLayoutParams(); + return carouselLayoutManager.getDecoratedMeasuredHeight(child) + params.topMargin + + params.bottomMargin; + } + @Override public void layoutDecoratedWithMargins(View child, int head, int tail) { + int top = getParentTop(); + int bottom = top + getDecoratedCrossAxisMeasurement(child); carouselLayoutManager.layoutDecoratedWithMargins( child, /* left= */ head, - /* top= */ getParentTop(), + /* top= */ top, /* right= */ tail, - /* bottom= */ getParentBottom()); + /* bottom= */ bottom); } @Override