UncontainedCarouselStrategy


Kotlin |Java

public final class UncontainedCarouselStrategy extends CarouselStrategy 


A CarouselStrategy that does not resize the original item width and fits as many as it can into the container, cutting off the rest. Cut off items may be resized in order to show an effect of items getting smaller at the ends.

Note that this strategy does not adjust the size of large items. Item widths are taken from the androidx.recyclerview.widget.RecyclerView item width.

This class will automatically be reversed by CarouselLayoutManager if being laid out right-to-left and does not need to make any account for layout direction itself.

For more information, see the component developer guidance and design guidelines.

Summary

Public constructors

Public methods

KeylineState

Calculates a keyline arrangement and returns a constructed KeylineState .

static float
getChildMaskPercentage(
float maskedSize,
float unmaskedSize,
float childMargins
)

Helper method to calculate a child's mask percentage given its masked size, unmasked size, and margins.

float

Returns the maximum small item size value.

float

Returns the minimum small item size value.

void
setSmallItemSizeMax(float maxSmallItemSize)

Sets the maximum size for the small items.

void
setSmallItemSizeMin(float minSmallItemSize)

Sets the minimum size for the small items.

boolean
shouldRefreshKeylineState(Carousel carousel, int oldItemCount)

Whether or not the strategy keylines should be refreshed based on the old item count and the carousel's current parameters.

Inherited methods

From com.google.android.material.carousel.CarouselStrategy

Public constructors

UncontainedCarouselStrategy

public UncontainedCarouselStrategy()

Public methods

onFirstChildMeasuredWithMargins

public KeylineState onFirstChildMeasuredWithMargins(Carousel carousel, View child)

Calculates a keyline arrangement and returns a constructed KeylineState .

This method is called when Carousel measures the first item to be added to its scroll container. This method then must create a KeylineState which tells Carousel how to fill the scroll container with items - how many are visible at once, what their sizes are, and where they're placed.

For example, take a simple arrangement that fills the scroll container with two large items and one small item. As the user scrolls the first large item moves off-screen to the left, the second large item moves to position 1, the small item unmasks into a large item at position 2, and a new small item scrolls into view from the right. To create this arrangement, pick any size for the small item that will be smaller than the large item. Next, take the carousel's total space, subtract the small item size and divide the remainder by two - this is your large item size. After determining the size of our large and small items, we can now construct a KeylineState and add keylines representing each item:

// Find the centers of the items in our arrangement, aligning the first item's left with the
// left of the scroll container (0).
floatfirstLargeItemCenter=largeChildSize/2F;
floatsmallItemCenter=(largeChildSize*2F)+(smallChildSize/2F);
// Get our child margins to use when calculating mask percentage
LayoutParamschildLayoutParams=(LayoutParams)child.getLayoutParams();
floatchildMargins=childLayoutParams.leftMargin+childLayoutParams.rightMargin;
returnnewKeylineState.Builder(largeChildWidth)
.addKeylineRange(
firstLargeItemCenter,// offsetLoc
getChildMaskPercentage(largeChildSize,largeChildSize,childMargins),// mask
largeChildSize,// maskedItemSize
2,// count
true)// isFocal
.addKeyline(
smallItemCenter,// offsetLoc
getChildMaskPercentage(smallChildSize,largeChildSize,childMargins),// mask
smallChildSize);// maskedItemSize

A strategy does not need to take layout direction into account. CarouselLayoutManager automatically reverses the strategy's KeylineState when laid out in right-to-left. Additionally, CarouselLayoutManager shifts the focal keylines to the start or end of the container when at the start or end of a list in order to allow every item in the list to pass through the focal state.

For additional guidelines on constructing valid KeylineStates, see KeylineState.Builder .

Parameters
Carousel carousel

The carousel to create a KeylineState for

View child

The first measured view from the carousel.

Returns
KeylineState

A KeylineState to be used by the layout manager to offset and mask children along the scrolling axis.

Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2026年05月14日 UTC.