# CarouselStrategy

[View Source](https://github.com/material-components/material-components-android/blob/1.14.0/lib/java/com/google/android/material/carousel/CarouselStrategy.java)

*** ** * ** ***


```
public abstract class CarouselStrategy
```

<br />

Known direct subclasses [FullScreenCarouselStrategy](https://developer.android.com/reference/com/google/android/material/carousel/FullScreenCarouselStrategy), [HeroCarouselStrategy](https://developer.android.com/reference/com/google/android/material/carousel/HeroCarouselStrategy), [MultiBrowseCarouselStrategy](https://developer.android.com/reference/com/google/android/material/carousel/MultiBrowseCarouselStrategy), [UncontainedCarouselStrategy](https://developer.android.com/reference/com/google/android/material/carousel/UncontainedCarouselStrategy)

|---|---|
| `https://developer.android.com/reference/com/google/android/material/carousel/FullScreenCarouselStrategy` | A `https://developer.android.com/reference/com/google/android/material/carousel/CarouselStrategy` that fits one full-width or full-height item into a container to create a layout to browse one item at a time. |
| `https://developer.android.com/reference/com/google/android/material/carousel/HeroCarouselStrategy` | A `https://developer.android.com/reference/com/google/android/material/carousel/CarouselStrategy` that knows how to size and fit one large item and one small item into a container to create a layout to browse one 'hero' item at a time with a preview item. |
| `https://developer.android.com/reference/com/google/android/material/carousel/MultiBrowseCarouselStrategy` | A `https://developer.android.com/reference/com/google/android/material/carousel/CarouselStrategy` that knows how to size and fit large, medium and small items into a container to create a layout for quick browsing of multiple items at once. |
| `https://developer.android.com/reference/com/google/android/material/carousel/UncontainedCarouselStrategy` | A `https://developer.android.com/reference/com/google/android/material/carousel/CarouselStrategy` that does not resize the original item width and fits as many as it can into the container, cutting off the rest. |

*** ** * ** ***

A class responsible for creating a model used by a carousel to mask and offset views as they move along a scrolling axis.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/com/google/android/material/carousel/CarouselStrategy#CarouselStrategy()()` |

| ### Public methods |
|---|---|
| `static float` | `https://developer.android.com/reference/com/google/android/material/carousel/CarouselStrategy#getChildMaskPercentage(float,float,float)( float maskedSize, float unmaskedSize, float childMargins )` Helper method to calculate a child's mask percentage given its masked size, unmasked size, and margins. |
| `float` | `https://developer.android.com/reference/com/google/android/material/carousel/CarouselStrategy#getSmallItemSizeMax()()` Returns the maximum small item size value. |
| `float` | `https://developer.android.com/reference/com/google/android/material/carousel/CarouselStrategy#getSmallItemSizeMin()()` Returns the minimum small item size value. |
| `abstract https://developer.android.com/reference/com/google/android/material/carousel/KeylineState` | `https://developer.android.com/reference/com/google/android/material/carousel/CarouselStrategy#onFirstChildMeasuredWithMargins(com.google.android.material.carousel.Carousel,View)(https://developer.android.com/reference/com/google/android/material/carousel/Carousel carousel, View child)` Calculates a keyline arrangement and returns a constructed `https://developer.android.com/reference/com/google/android/material/carousel/KeylineState`. |
| `void` | `https://developer.android.com/reference/com/google/android/material/carousel/CarouselStrategy#setSmallItemSizeMax(float)(float maxSmallItemSize)` Sets the maximum size for the small items. |
| `void` | `https://developer.android.com/reference/com/google/android/material/carousel/CarouselStrategy#setSmallItemSizeMin(float)(float minSmallItemSize)` Sets the minimum size for the small items. |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/carousel/CarouselStrategy#shouldRefreshKeylineState(com.google.android.material.carousel.Carousel,int)(https://developer.android.com/reference/com/google/android/material/carousel/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. |

## Public constructors

### CarouselStrategy

```
public CarouselStrategy()
```

## Public methods

### getChildMaskPercentage

```
public 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.

| Parameters |
|---|---|
| `float maskedSize` | The size this method calculates a mask percentage for |
| `float unmaskedSize` | The size this child is when fully unmasked (mask == 0F). This should likely be the `itemSize` passed to the `https://developer.android.com/reference/com/google/android/material/carousel/KeylineState.Builder` constructor. |
| `float childMargins` | The total margins at the start+end or top+bottom of this child. By default, these are removed from the returned mask as margins should not change in size as a child's mask changes. |

| Returns |
|---|---|
| `float` | A percentage by which the child should be masked in order to be sized at ` maskedSize`. 0F is fully unmasked and 1F is fully masked. |

### getSmallItemSizeMax

```
public float getSmallItemSizeMax()
```

Returns the maximum small item size value.

### getSmallItemSizeMin

```
public float getSmallItemSizeMin()
```

Returns the minimum small item size value.

### onFirstChildMeasuredWithMargins

```
public abstract KeylineState onFirstChildMeasuredWithMargins(Carousel carousel, View child)
```

Calculates a keyline arrangement and returns a constructed `https://developer.android.com/reference/com/google/android/material/carousel/KeylineState`.

This method is called when `https://developer.android.com/reference/com/google/android/material/carousel/Carousel` measures the first item to be added to its scroll container. This method then must create a `https://developer.android.com/reference/com/google/android/material/carousel/KeylineState` which tells `https://developer.android.com/reference/com/google/android/material/carousel/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 `https://developer.android.com/reference/com/google/android/material/carousel/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).
float firstLargeItemCenter = largeChildSize / 2F;
float smallItemCenter = (largeChildSize * 2F) + (smallChildSize / 2F);

// Get our child margins to use when calculating mask percentage
LayoutParams childLayoutParams = (LayoutParams) child.getLayoutParams();
float childMargins = childLayoutParams.leftMargin + childLayoutParams.rightMargin;

return new KeylineState.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. `https://developer.android.com/reference/com/google/android/material/carousel/CarouselLayoutManager` automatically reverses the strategy's `https://developer.android.com/reference/com/google/android/material/carousel/KeylineState` when laid out in right-to-left. Additionally, `https://developer.android.com/reference/com/google/android/material/carousel/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 `https://developer.android.com/reference/com/google/android/material/carousel/KeylineState.Builder`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/com/google/android/material/carousel/Carousel carousel` | The carousel to create a `https://developer.android.com/reference/com/google/android/material/carousel/KeylineState` for |
| `View child` | The first measured view from the carousel. |

| Returns |
|---|---|
| `https://developer.android.com/reference/com/google/android/material/carousel/KeylineState` | A `https://developer.android.com/reference/com/google/android/material/carousel/KeylineState` to be used by the layout manager to offset and mask children along the scrolling axis. |

### setSmallItemSizeMax

```
public void setSmallItemSizeMax(float maxSmallItemSize)
```

Sets the maximum size for the small items.

This method is a no-op for strategies that do not have small items.

Note that setting this size may impact other sizes in the carousel in order to fit the carousel strategy configuration.

| Parameters |
|---|---|
| `float maxSmallItemSize` | size to set the small item to. |

### setSmallItemSizeMin

```
public void setSmallItemSizeMin(float minSmallItemSize)
```

Sets the minimum size for the small items.

This method is a no-op for strategies that do not have small items.

Note that setting this size may impact other sizes in the carousel in order to fit the carousel strategy configuration.

| Parameters |
|---|---|
| `float minSmallItemSize` | size to set the small item to. |

### shouldRefreshKeylineState

```
public 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. This method is called when the item count is updated, and is used to update the keyline strategy when the item count is less than the number of keylines in the normal keyline strategy.

| Returns |
|---|---|
| `boolean` | true if the keylines should be refreshed. |