TransformingLazyColumnState

class TransformingLazyColumnState : ScrollableState


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

In most cases, this will be created via rememberTransformingLazyColumnState.

Summary

Public constructors

Public functions

suspend Unit
animateScrollToItem(index: @IntRange(from = 0) Int, scrollOffset: Int)

Animate (smooth scroll) to the given item.

open Float
Unit
requestScrollToItem(index: @IntRange(from = 0) Int, scrollOffset: Int)

Requests the item at index to be at the center of the viewport during the next remeasure, offset by scrollOffset.

open suspend Unit
scroll(scrollPriority: MutatePriority, block: suspend ScrollScope.() -> Unit)
suspend Unit
scrollToItem(index: @IntRange(from = 0) Int, scrollOffset: Int)

Scrolls the item specified by index to the center of the screen.

Public properties

Int

The index of the item that is used in scrolling.

Int

The scroll offset of the anchor item.

open Boolean
open Boolean
open Boolean
TransformingLazyColumnLayoutInfo

The object of LazyColumnLayoutInfo calculated during the last layout pass.

Public constructors

TransformingLazyColumnState

Added in 1.5.0-alpha07
TransformingLazyColumnState()

Public functions

animateScrollToItem

Added in 1.5.0-alpha07
suspend fun animateScrollToItem(index: @IntRange(from = 0) Int, scrollOffset: Int = 0): Unit

Animate (smooth scroll) to the given item.

The scroll position anchorItemIndex and anchorItemScrollOffset will be updated to take into account the new layout. There is no guarantee that index will become the new anchorItemIndex since requested scrollOffset may position item with another index closer to the anchor point.

Parameters
index: @IntRange(from = 0) Int

the index to which to scroll. Must be non-negative.

scrollOffset: Int = 0

The offset between the center of the screen and item's center. Positive offset means the item will be scrolled up.

dispatchRawDelta

Added in 1.5.0-alpha07
open fun dispatchRawDelta(delta: Float): Float

requestScrollToItem

Added in 1.5.0-alpha07
fun requestScrollToItem(index: @IntRange(from = 0) Int, scrollOffset: Int = 0): Unit

Requests the item at index to be at the center of the viewport during the next remeasure, offset by scrollOffset.

The scroll position anchorItemIndex and anchorItemScrollOffset will be updated to take into account the new layout. There is no guarantee that index will become the new anchorItemIndex since requested scrollOffset may position item with another index closer to the anchor point.

The scroll position will be updated to the requested position rather than maintain the index based on the center 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: @IntRange(from = 0) Int

the index to which to scroll. Must be non-negative.

scrollOffset: Int = 0

The offset between the center of the screen and item's center. Positive offset means the item will be scrolled up.

scroll

Added in 1.5.0-alpha07
open suspend fun scroll(scrollPriority: MutatePriority, block: suspend ScrollScope.() -> Unit): Unit

scrollToItem

Added in 1.5.0-alpha07
suspend fun scrollToItem(index: @IntRange(from = 0) Int, scrollOffset: Int = 0): Unit

Scrolls the item specified by index to the center of the screen.

The scroll position anchorItemIndex and anchorItemScrollOffset will be updated to take into account the new layout. There is no guarantee that index will become the new anchorItemIndex since requested scrollOffset may position item with another index closer to the anchor point.

This operation happens instantly without animation.

Parameters
index: @IntRange(from = 0) Int

The index of the item to scroll to. Must be non-negative.

scrollOffset: Int = 0

The offset between the center of the screen and item's center. Positive offset means the item will be scrolled up.

Public properties

anchorItemIndex

Added in 1.5.0-alpha07
val anchorItemIndexInt

The index of the item that is used in scrolling. For the most cases that is the item closest to the center of viewport from TransformingLazyColumnLayoutInfo.visibleItems, however it might change during scroll.

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 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:

import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.remember
import androidx.wear.compose.foundation.lazy.TransformingLazyColumn
import androidx.wear.compose.foundation.lazy.TransformingLazyColumnState
import androidx.wear.compose.foundation.lazy.rememberTransformingLazyColumnState

val columnState = rememberTransformingLazyColumnState()
val isAtCenter by remember {
    derivedStateOf {
        columnState.anchorItemIndex == 0 && columnState.anchorItemScrollOffset == 0
    }
}

@Composable
fun ScrollToTopButton(@Suppress("UNUSED_PARAMETER") columnState: TransformingLazyColumnState) {}

if (!isAtCenter) {
    ScrollToTopButton(columnState)
}

anchorItemScrollOffset

Added in 1.5.0-alpha07
val anchorItemScrollOffsetInt

The scroll offset of the anchor 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.

See also
anchorItemIndex

for samples with the recommended usage patterns.

canScrollBackward

open val canScrollBackwardBoolean

canScrollForward

open val canScrollForwardBoolean

isScrollInProgress

Added in 1.5.0-alpha07
open val isScrollInProgressBoolean

layoutInfo

Added in 1.5.0-alpha07
val layoutInfoTransformingLazyColumnLayoutInfo

The object of LazyColumnLayoutInfo 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":