🌈 Create new beautiful Compose Gradients with our new wesite ->
Class

LazyListState

A state object that can be hoisted to control and observe scrolling.

Source set: Common
public class LazyListState public constructor(
    firstVisibleItemIndex: Int = 0,
    firstVisibleItemScrollOffset: Int = 0,
    internal val prefetchStrategy: LazyListPrefetchStrategy = LazyListPrefetchStrategy(),
) : ScrollableState

A state object that can be hoisted to control and observe scrolling.

In most cases, this will be created via rememberLazyListState.

Parameters

firstVisibleItemIndex the initial value for LazyListState.firstVisibleItemIndex
firstVisibleItemScrollOffset the initial value for LazyListState.firstVisibleItemScrollOffset
prefetchStrategy the LazyListPrefetchStrategy to use for prefetching content in this list

Properties

firstVisibleItemIndex

Source set: Common
public val firstVisibleItemIndex: Int

The index of the first item that is visible within the scrollable viewport area not including items in the content padding region. For the first visible item that includes items in the content padding please use LazyListLayoutInfo.visibleItemsInfo.

Note that this property is observable and if you use it in the composable function it will be recomposed on every change causing potential performance issues.

If you want to run some side effects like sending an analytics event or updating a state based on this value consider using "snapshotFlow":

If you need to use it in the composition then consider wrapping the calculation into a derived state in order to only have recompositions when the derived value changes:

firstVisibleItemScrollOffset

Source set: Common
public val firstVisibleItemScrollOffset: Int

The scroll offset of the first visible item. Scrolling forward is positive - i.e., the amount that the item is offset backwards.

Note that this property is observable and if you use it in the composable function it will be recomposed on every scroll causing potential performance issues.

layoutInfo

Source set: Common
public val layoutInfo: LazyListLayoutInfo

The object of LazyListLayoutInfo calculated during the last layout pass. For example, you can use it to calculate what items are currently visible.

Note that this property is observable and is updated after every scroll or remeasure. If you use it in the composable function it will be recomposed on every change causing potential performance issues including infinity recomposition loop. Therefore, avoid using it in the composition.

If you want to run some side effects like sending an analytics event or updating a state based on this value consider using "snapshotFlow":

interactionSource

Source set: Common
public val interactionSource: InteractionSource

InteractionSource that will be used to dispatch drag events when this list is being dragged. If you want to know whether the fling (or animated scroll) is in progress, use isScrollInProgress.

isScrollInProgress

Source set: Common
override val isScrollInProgress: Boolean

canScrollForward

Source set: Common
override var canScrollForward: Boolean by mutableStateOf(false)

canScrollBackward

Source set: Common
override var canScrollBackward: Boolean by mutableStateOf(false)

lastScrolledForward

Source set: Common
override val lastScrolledForward: Boolean

lastScrolledBackward

Source set: Common
override val lastScrolledBackward: Boolean

scrollIndicatorState

Source set: Common
override val scrollIndicatorState: ScrollIndicatorState?

Functions

scrollToItem

Source set: Common
public suspend fun scrollToItem(@AndroidXIntRange(from = 0) index: Int, scrollOffset: Int = 0)

Instantly brings the item at index to the top of the viewport, offset by scrollOffset pixels.

Parameters

index the index to which to scroll. Must be non-negative.
scrollOffset the offset that the item should end up after the scroll. Note that positive offset refers to forward scroll, so in a top-to-bottom list, positive offset will scroll the item further upward (taking it partly offscreen).

requestScrollToItem

Source set: Common
public fun requestScrollToItem(@AndroidXIntRange(from = 0) index: Int, scrollOffset: Int = 0)

Requests the item at index to be at the start of the viewport during the next remeasure, offset by scrollOffset, and schedules a remeasure.

The scroll position will be updated to the requested position rather than maintain the index based on the first visible item key (when a data set change will also be applied during the next remeasure), but only for the next remeasure.

Any scroll in progress will be cancelled.

Parameters

index the index to which to scroll. Must be non-negative.
scrollOffset the offset that the item should end up after the scroll. Note that positive offset refers to forward scroll, so in a top-to-bottom list, positive offset will scroll the item further upward (taking it partly offscreen).

scroll

Source set: Common
override suspend fun scroll(
        scrollPriority: MutatePriority,
        block: suspend ScrollScope.() -> Unit,
    )

Call this function to take control of scrolling and gain the ability to send scroll events via ScrollScope.scrollBy. All actions that change the logical scroll position must be performed within a scroll block (even if they don't call any other methods on this object) in order to guarantee that mutual exclusion is enforced.

If scroll is called from elsewhere, this will be canceled.

dispatchRawDelta

Source set: Common
override fun dispatchRawDelta(delta: Float): Float

animateScrollToItem

Source set: Common
public suspend fun animateScrollToItem(
        @AndroidXIntRange(from = 0) index: Int,
        scrollOffset: Int = 0,
    )

Animate (smooth scroll) to the given item.

Parameters

index the index to which to scroll. Must be non-negative.
scrollOffset the offset that the item should end up after the scroll. Note that positive offset refers to forward scroll, so in a top-to-bottom list, positive offset will scroll the item further upward (taking it partly offscreen).

Members

Companion

Source set: Common
public companion object

Properties

Saver

Source set: Common
public val Saver: Saver<LazyListState, *> =
            listSaver(
                save = { listOf(it.firstVisibleItemIndex, it.firstVisibleItemScrollOffset) },
                restore = {
                    LazyListState(
                        firstVisibleItemIndex = it[0],
                        firstVisibleItemScrollOffset = it[1],
                    )
                },
            )

The default Saver implementation for LazyListState.