Skip to content

Commit 916e908

Browse files
imhappikendrickumstattd
authored andcommitted
[Carousel] Update multi-browse strategy to always have at least 1 medium item
PiperOrigin-RevId: 719372980
1 parent 85c4e0f commit 916e908

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

Diff for: lib/java/com/google/android/material/carousel/MultiBrowseCarouselStrategy.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public KeylineState onFirstChildMeasuredWithMargins(
9595
// then finally 1.
9696

9797
int[] smallCounts = SMALL_COUNTS;
98-
if (carouselSize < smallChildSizeMin * 2) {
98+
if (carouselSize <= smallChildSizeMin * 2) {
9999
// If the available space is too small to fit a large item and small item and a large item
100100
// (large items must be at least as big as a small item), allow arrangements with no small
101101
// items.
@@ -134,7 +134,19 @@ public KeylineState onFirstChildMeasuredWithMargins(
134134

135135
keylineCount = arrangement.getItemCount();
136136

137-
if (ensureArrangementFitsItemCount(arrangement, carousel.getItemCount())) {
137+
boolean refreshArrangement =
138+
ensureArrangementFitsItemCount(arrangement, carousel.getItemCount());
139+
140+
// Ensure that the arrangement is never only filled with large items unless there's no space for
141+
// 2 small item.
142+
if (arrangement.mediumCount == 0
143+
&& arrangement.smallCount == 0
144+
&& carouselSize > 2 * smallChildSizeMin) {
145+
arrangement.smallCount = 1;
146+
refreshArrangement = true;
147+
}
148+
149+
if (refreshArrangement) {
138150
// In case counts changed after ensuring the previous arrangement fit the item
139151
// counts, we call `findLowestCostArrangement` again with the item counts set.
140152
arrangement =

Diff for: lib/javatests/com/google/android/material/carousel/MultiBrowseCarouselStrategyTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,22 @@ public void testSmallContainer_shouldShowOneLargeItem() {
9595
assertThat(keylineState.getKeylines().get(1).maskedItemSize).isEqualTo((float) carouselWidth);
9696
}
9797

98+
@Test
99+
public void testContainer_shouldShowOneLargeOneSmallItem() {
100+
View view = createViewWithSize(ApplicationProvider.getApplicationContext(), 100, 400);
101+
float minSmallItemSize =
102+
view.getResources().getDimension(R.dimen.m3_carousel_small_item_size_min);
103+
// Create a carousel that will fit at least one small item and one larger item
104+
int carouselWidth = ((int) (minSmallItemSize * 2f)) + 1;
105+
Carousel carousel = createCarouselWithWidth(carouselWidth);
106+
107+
MultiBrowseCarouselStrategy config = setupStrategy();
108+
KeylineState keylineState = config.onFirstChildMeasuredWithMargins(carousel, view);
109+
110+
assertThat(keylineState.getKeylines()).hasSize(4);
111+
assertThat(keylineState.getKeylines().get(2).maskedItemSize).isEqualTo(minSmallItemSize);
112+
}
113+
98114
@Test
99115
public void testKnownArrangementWithMediumItem_correctlyCalculatesKeylineLocations() {
100116
float[] locOffsets = new float[] {-.5F, 100F, 300F, 464F, 556F, 584.5F};

0 commit comments

Comments
 (0)