public class CarouselState(
currentItem: Int = 0,
@FloatRange(from = -0.5, to = 0.5) currentItemOffsetFraction: Float = 0f,
itemCount: () -> Int,
) : ScrollableState
The state that can be used to control all types of carousels.
Parameters
| currentItem | the current item to be scrolled to. |
| currentItemOffsetFraction | the offset of the current item as a fraction of the item's size. This should vary between -0.5 and 0.5 and indicates how to offset the current item from the snapped position. |
| itemCount | the number of items this Carousel will have. |
Properties
isScrollInProgress
override val isScrollInProgress: Boolean
currentItem
public val currentItem: Int
The item that sits closest to the snapped position. This is an observable value and will change as the carousel scrolls either by gesture or animation.
Please refer to PagerState.currentPage for more information.
Functions
dispatchRawDelta
override fun dispatchRawDelta(delta: Float): Float
scroll
override suspend fun scroll(
scrollPriority: MutatePriority,
block: suspend ScrollScope.() -> Unit,
)
scrollToItem
public suspend fun scrollToItem(item: Int): Unit
Scroll (jump immediately) to a given item.
Parameters
| item | The destination item to scroll to |
animateScrollToItem
public suspend fun animateScrollToItem(
item: Int,
animationSpec: AnimationSpec<Float> = spring(),
): Unit
Scroll animate to a given item. If the item is too far away from currentItem, Carousel will avoid composing all intermediate items by jumping to a nearer item before animating the scroll.
Please refer to the sample to learn how to use this API.
Parameters
| item | the index of the item to scroll to with an animation |
| animationSpec | an AnimationSpec used to scroll between the items. |
Members
Companion
public companion object
Properties
Saver
public val Saver: Saver<CarouselState, *> =
listSaver(
save = {
listOf(
it.pagerState.currentPage,
it.pagerState.currentPageOffsetFraction,
it.pagerState.pageCount,
)
},
restore = {
CarouselState(
currentItem = it[0] as Int,
currentItemOffsetFraction = it[1] as Float,
itemCount = { it[2] as Int },
)
},
)
To keep current item and item offset saved