Skip to content

Commit

Permalink
[Carousel] Fixed strategies crashing when there is not enough availab…
Browse files Browse the repository at this point in the history
…le space for a large and a small item

If the carousel container is not large enough to fit a small item and large item that is at least as large as the minimum small item size, mutli-browse and hero strategies will create an arrangment with a single, large item.

PiperOrigin-RevId: 568610428
  • Loading branch information
hunterstich authored and afohrman committed Sep 27, 2023
1 parent 98439df commit c418063
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Expand Up @@ -82,6 +82,11 @@ KeylineState onFirstChildMeasuredWithMargins(@NonNull Carousel carousel, @NonNul
getSmallSizeMax(child.getContext()) + childMargins);
float targetMediumChildSize = (targetLargeChildSize + targetSmallChildSize) / 2F;

int[] smallCounts = SMALL_COUNTS;
if (availableSpace < smallChildSizeMin * 2) {
smallCounts = new int[] { 0 };
}

// Find the minimum space left for large items after filling the carousel with the most
// permissible small items to determine a plausible minimum large count.
float minAvailableLargeSpace = availableSpace - (smallChildSizeMax * maxValue(SMALL_COUNTS));
Expand All @@ -100,8 +105,8 @@ KeylineState onFirstChildMeasuredWithMargins(@NonNull Carousel carousel, @NonNul
smallChildSizeMin,
smallChildSizeMax,
isCenterAligned
? doubleCounts(SMALL_COUNTS)
: SMALL_COUNTS,
? doubleCounts(smallCounts)
: smallCounts,
targetMediumChildSize,
isCenterAligned
? doubleCounts(MEDIUM_COUNTS)
Expand Down
Expand Up @@ -118,11 +118,19 @@ KeylineState onFirstChildMeasuredWithMargins(@NonNull Carousel carousel, @NonNul
// then finally 1.

int[] smallCounts = SMALL_COUNTS;
if (availableSpace < smallChildSizeMin * 2) {
// If the available space is too small to fit a large item and small item and a large item
// (large items must be at least as big as a small item), allow arrangements with no small
// items.
smallCounts = new int[] { 0 };
}

int[] mediumCounts = forceCompactArrangement ? MEDIUM_COUNTS_COMPACT : MEDIUM_COUNTS;
if (carousel.getCarouselAlignment() == CarouselLayoutManager.ALIGNMENT_CENTER) {
smallCounts = doubleCounts(smallCounts);
mediumCounts = doubleCounts(mediumCounts);
}

// Find the minimum space left for large items after filling the carousel with the most
// permissible medium and small items to determine a plausible minimum large count.
float minAvailableLargeSpace =
Expand Down

0 comments on commit c418063

Please sign in to comment.