Skip to content

Commit

Permalink
[Carousel] Added support for cross axis wrap_content RecyclerViews
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 590637698
  • Loading branch information
hunterstich authored and raajkumars committed Dec 14, 2023
1 parent 6310654 commit e88a1b9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

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

0 comments on commit e88a1b9

Please sign in to comment.