Skip to content

Commit e88a1b9

Browse files
hunterstichraajkumars
authored andcommittedDec 14, 2023
[Carousel] Added support for cross axis wrap_content RecyclerViews
PiperOrigin-RevId: 590637698
1 parent 6310654 commit e88a1b9

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed
 

‎lib/java/com/google/android/material/carousel/CarouselLayoutManager.java

+5
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ public void onLayoutChildren(Recycler recycler, State state) {
307307
lastItemCount = getItemCount();
308308
}
309309

310+
@Override
311+
public boolean isAutoMeasureEnabled() {
312+
return true;
313+
}
314+
310315
private void recalculateKeylineStateList(Recycler recycler) {
311316
View firstChild = recycler.getViewForPosition(0);
312317
measureChildWithMargins(firstChild, 0, 0);

‎lib/java/com/google/android/material/carousel/CarouselOrientationHelper.java

+34-4
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ static CarouselOrientationHelper createOrientationHelper(
8383
/** Returns the y-coordinate of the bottom edge of the parent recycler view. */
8484
abstract int getParentBottom();
8585

86+
87+
/**
88+
* Returns the space occupied by this View in the cross (non-scrolling) axis including
89+
* decorations and margins.
90+
*
91+
* @param child The view element to check
92+
* @return total space occupied by this view in the perpendicular orientation to current one
93+
*/
94+
abstract int getDecoratedCrossAxisMeasurement(View child);
95+
8696
/**
8797
* Helper method that calls {@link CarouselLayoutManager#layoutDecoratedWithMargins(View, int,
8898
* int, int, int)} with the correct coordinates according to the orientation.
@@ -182,13 +192,23 @@ int getParentBottom() {
182192
return carouselLayoutManager.getHeight();
183193
}
184194

195+
@Override
196+
int getDecoratedCrossAxisMeasurement(View child) {
197+
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
198+
child.getLayoutParams();
199+
return carouselLayoutManager.getDecoratedMeasuredWidth(child) + params.leftMargin
200+
+ params.rightMargin;
201+
}
202+
185203
@Override
186204
public void layoutDecoratedWithMargins(View child, int head, int tail) {
205+
int left = getParentLeft();
206+
int right = left + getDecoratedCrossAxisMeasurement(child);
187207
carouselLayoutManager.layoutDecoratedWithMargins(
188208
child,
189-
/* left= */ getParentLeft(),
209+
/* left= */ left,
190210
/* top= */ head,
191-
/* right= */ getParentRight(),
211+
/* right= */ right,
192212
/* bottom= */ tail);
193213
}
194214

@@ -276,14 +296,24 @@ int getParentBottom() {
276296
return carouselLayoutManager.getHeight() - carouselLayoutManager.getPaddingBottom();
277297
}
278298

299+
@Override
300+
int getDecoratedCrossAxisMeasurement(View child) {
301+
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
302+
child.getLayoutParams();
303+
return carouselLayoutManager.getDecoratedMeasuredHeight(child) + params.topMargin
304+
+ params.bottomMargin;
305+
}
306+
279307
@Override
280308
public void layoutDecoratedWithMargins(View child, int head, int tail) {
309+
int top = getParentTop();
310+
int bottom = top + getDecoratedCrossAxisMeasurement(child);
281311
carouselLayoutManager.layoutDecoratedWithMargins(
282312
child,
283313
/* left= */ head,
284-
/* top= */ getParentTop(),
314+
/* top= */ top,
285315
/* right= */ tail,
286-
/* bottom= */ getParentBottom());
316+
/* bottom= */ bottom);
287317
}
288318

289319
@Override

0 commit comments

Comments
 (0)
Please sign in to comment.