{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

LazyStaggeredGridState

public final class LazyStaggeredGridState implements ScrollableState


Hoisted state object controlling LazyVerticalStaggeredGrid or LazyHorizontalStaggeredGrid. In most cases, it should be created via rememberLazyStaggeredGridState.

Summary

Nested types

Public constructors

LazyStaggeredGridState(
    int initialFirstVisibleItemIndex,
    int initialFirstVisibleItemOffset
)

Public methods

final void
animateScrollToItem(int index, int scrollOffset)

Animate (smooth scroll) to the given item.

float
dispatchRawDelta(float delta)

Dispatch scroll delta in pixels avoiding all scroll related mechanisms.

boolean

Whether this ScrollableState can scroll backward (consume a negative delta).

boolean

Whether this ScrollableState can scroll forward (consume a positive delta).

final int

Index of the first visible item across all staggered grid lanes.

final int

Current offset of the item with firstVisibleItemIndex relative to the container start.

final @NonNull InteractionSource

InteractionSource that will be used to dispatch drag events when this list is being dragged.

final @NonNull LazyStaggeredGridLayoutInfo

Layout information calculated during last layout pass, with information about currently visible items and container parameters.

boolean

Whether this scrollableState is currently scrolling by gesture, fling or programmatically or not.

void
scroll(
    @NonNull MutatePriority scrollPriority,
    @ExtensionFunctionType @NonNull SuspendFunction1<@NonNull ScrollScopeUnit> block
)

Call this function to take control of scrolling and gain the ability to send scroll events via ScrollScope.scrollBy.

final void
scrollToItem(int index, int scrollOffset)

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

Public constructors

LazyStaggeredGridState

public LazyStaggeredGridState(
    int initialFirstVisibleItemIndex,
    int initialFirstVisibleItemOffset
)
Parameters
int initialFirstVisibleItemIndex

initial value for firstVisibleItemIndex

int initialFirstVisibleItemOffset

initial value for firstVisibleItemScrollOffset

Public methods

animateScrollToItem

public final void animateScrollToItem(int index, int scrollOffset)

Animate (smooth scroll) to the given item.

Parameters
int index

the index to which to scroll. MUST NOT be negative.

int 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).

dispatchRawDelta

public float dispatchRawDelta(float delta)

Dispatch scroll delta in pixels avoiding all scroll related mechanisms.

NOTE: unlike scroll, dispatching any delta with this method won't trigger nested scroll, won't stop ongoing scroll/drag animation and will bypass scrolling of any priority. This method will also ignore reverseDirection and other parameters set in scrollable.

This method is used internally for nested scrolling dispatch and other low level operations, allowing implementers of ScrollableState influence the consumption as suits them. Manually dispatching delta via this method will likely result in a bad user experience, you must prefer scroll method over this one.

Parameters
float delta

amount of scroll dispatched in the nested scroll process

Returns
float

the amount of delta consumed

getCanScrollBackward

public boolean getCanScrollBackward()

Whether this ScrollableState can scroll backward (consume a negative delta). This is typically false if the scroll position is equal to its minimum value, and true otherwise.

Note that true here does not imply that delta will be consumed - the ScrollableState may decide not to handle the incoming delta (such as if it is already being scrolled separately). Additionally, for backwards compatibility with previous versions of ScrollableState this value defaults to true.

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.filled.KeyboardArrowUp
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer

val state = rememberLazyListState()
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
    Icon(
        Icons.Filled.KeyboardArrowUp,
        null,
        Modifier.graphicsLayer {
            // Hide the icon if we cannot scroll backward (we are the start of the list)
            // We use graphicsLayer here to control the alpha so that we only redraw when this
            // value changes, instead of recomposing
            alpha = if (state.canScrollBackward) 1f else 0f
        },
        Color.Red
    )
    val items = (1..100).toList()
    LazyColumn(
        Modifier
            .weight(1f)
            .fillMaxWidth(), state
    ) {
        items(items) {
            Text("Item is $it")
        }
    }
    Icon(
        Icons.Filled.KeyboardArrowDown,
        null,
        Modifier.graphicsLayer {
            // Hide the icon if we cannot scroll forward (we are the end of the list)
            // We use graphicsLayer here to control the alpha so that we only redraw when this
            // value changes, instead of recomposing
            alpha = if (state.canScrollForward) 1f else 0f
        },
        Color.Red
    )
}

getCanScrollForward

public boolean getCanScrollForward()

Whether this ScrollableState can scroll forward (consume a positive delta). This is typically false if the scroll position is equal to its maximum value, and true otherwise.

Note that true here does not imply that delta will be consumed - the ScrollableState may decide not to handle the incoming delta (such as if it is already being scrolled separately). Additionally, for backwards compatibility with previous versions of ScrollableState this value defaults to true.

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.filled.KeyboardArrowUp
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer

val state = rememberLazyListState()
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
    Icon(
        Icons.Filled.KeyboardArrowUp,
        null,
        Modifier.graphicsLayer {
            // Hide the icon if we cannot scroll backward (we are the start of the list)
            // We use graphicsLayer here to control the alpha so that we only redraw when this
            // value changes, instead of recomposing
            alpha = if (state.canScrollBackward) 1f else 0f
        },
        Color.Red
    )
    val items = (1..100).toList()
    LazyColumn(
        Modifier
            .weight(1f)
            .fillMaxWidth(), state
    ) {
        items(items) {
            Text("Item is $it")
        }
    }
    Icon(
        Icons.Filled.KeyboardArrowDown,
        null,
        Modifier.graphicsLayer {
            // Hide the icon if we cannot scroll forward (we are the end of the list)
            // We use graphicsLayer here to control the alpha so that we only redraw when this
            // value changes, instead of recomposing
            alpha = if (state.canScrollForward) 1f else 0f
        },
        Color.Red
    )
}

getFirstVisibleItemIndex

public final int getFirstVisibleItemIndex()

Index of the first visible item across all staggered grid lanes.

This property is observable and when use it in composable function it will be recomposed on each scroll, potentially causing performance issues.

getFirstVisibleItemScrollOffset

public final int getFirstVisibleItemScrollOffset()

Current offset of the item with firstVisibleItemIndex relative to the container start.

This property is observable and when use it in composable function it will be recomposed on each scroll, potentially causing performance issues.

getInteractionSource

public final @NonNull InteractionSource getInteractionSource()

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.

getLayoutInfo

public final @NonNull LazyStaggeredGridLayoutInfo getLayoutInfo()

Layout information calculated during last layout pass, with information about currently visible items and container parameters.

This property is observable and when use it in composable function it will be recomposed on each scroll, potentially causing performance issues.

isScrollInProgress

public boolean isScrollInProgress()

Whether this scrollableState is currently scrolling by gesture, fling or programmatically or not.

scroll

public void scroll(
    @NonNull MutatePriority scrollPriority,
    @ExtensionFunctionType @NonNull SuspendFunction1<@NonNull ScrollScopeUnit> block
)

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.

scrollToItem

public final void scrollToItem(int index, int scrollOffset)

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

Parameters
int index

the index to which to scroll. MUST NOT be negative.

int scrollOffset

the offset where the item should end up after the scroll. Note that positive offset refers to forward scroll, so in a reversed list, positive offset will scroll the item further upward (taking it partly offscreen).