androidx.compose.foundation.lazy.grid

Interfaces

GridCells

This class describes the count and the sizes of columns in vertical grids, or rows in horizontal grids.

Cmn
LazyGridItemInfo

Contains useful information about an individual item in lazy grids like LazyVerticalGrid.

Cmn
LazyGridItemScope

Receiver scope being used by the item content parameter of LazyVerticalGrid.

Cmn
LazyGridItemSpanScope

Scope of lambdas used to calculate the spans of items in lazy grids.

Cmn
LazyGridLayoutInfo

Contains useful information about the currently displayed layout state of lazy grids like LazyVerticalGrid.

Cmn
LazyGridPrefetchResultScope

A scope for LazyGridPrefetchScope.scheduleLinePrefetch callbacks.

Cmn
LazyGridPrefetchScope

Scope for callbacks in LazyGridPrefetchStrategy which allows prefetches to be requested.

Cmn
LazyGridPrefetchStrategy

Implementations of this interface control which indices of a LazyGrid should be prefetched (precomposed and premeasured during idle time) as the user interacts with it.

Cmn
LazyGridScope

Receiver scope which is used by LazyVerticalGrid.

Cmn

Classes

GridCells.Adaptive

Defines a grid with as many rows or columns as possible on the condition that every cell has at least minSize space and all extra space distributed evenly.

Cmn
GridCells.Fixed

Defines a grid with fixed number of rows or columns.

Cmn
GridCells.FixedSize

Defines a grid with as many rows or columns as possible on the condition that every cell takes exactly size space.

Cmn
GridItemSpan

Represents the span of an item in a LazyVerticalGrid or a LazyHorizontalGrid.

Cmn
LazyGridState

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

Cmn

Annotations

LazyGridScopeMarker

DSL marker used to distinguish between lazy grid dsl scope and the item content scope.

Cmn

Composables

LazyHorizontalGrid

A lazy horizontal grid layout.

Cmn
LazyVerticalGrid

A lazy vertical grid layout.

Cmn
rememberLazyGridState

Creates a LazyGridState that is remembered across compositions.

Cmn

Top-level functions summary

GridItemSpan
GridItemSpan(currentLineSpan: @IntRange(from = 1) Int)

Creates a GridItemSpan with a specified currentLineSpan.

Cmn
LazyGridPrefetchStrategy

Creates an instance of the default LazyGridPrefetchStrategy, allowing for customization of the nested prefetch count.

Cmn
LazyLayoutScrollScope

An implementation of LazyLayoutScrollScope that can be used with LazyGrids.

Cmn

Extension functions summary

inline Unit
<T : Any?> LazyGridScope.items(
    items: Array<T>,
    noinline key: ((item) -> Any)?,
    noinline span: (LazyGridItemSpanScope.(item) -> GridItemSpan)?,
    noinline contentType: (item) -> Any?,
    crossinline itemContent: @Composable LazyGridItemScope.(item) -> Unit
)

Adds an array of items.

Cmn
inline Unit
<T : Any?> LazyGridScope.items(
    items: List<T>,
    noinline key: ((item) -> Any)?,
    noinline span: (LazyGridItemSpanScope.(item) -> GridItemSpan)?,
    noinline contentType: (item) -> Any?,
    crossinline itemContent: @Composable LazyGridItemScope.(item) -> Unit
)

Adds a list of items.

Cmn
inline Unit
<T : Any?> LazyGridScope.itemsIndexed(
    items: Array<T>,
    noinline key: ((index: Int, item) -> Any)?,
    noinline span: (LazyGridItemSpanScope.(index: Int, item) -> GridItemSpan)?,
    crossinline contentType: (index: Int, item) -> Any?,
    crossinline itemContent: @Composable LazyGridItemScope.(index: Int, item) -> Unit
)

Adds an array of items where the content of an item is aware of its index.

Cmn
inline Unit
<T : Any?> LazyGridScope.itemsIndexed(
    items: List<T>,
    noinline key: ((index: Int, item) -> Any)?,
    noinline span: (LazyGridItemSpanScope.(index: Int, item) -> GridItemSpan)?,
    crossinline contentType: (index: Int, item) -> Any?,
    crossinline itemContent: @Composable LazyGridItemScope.(index: Int, item) -> Unit
)

Adds a list of items where the content of an item is aware of its index.

Cmn

Top-level functions

GridItemSpan

fun GridItemSpan(currentLineSpan: @IntRange(from = 1) Int): GridItemSpan

Creates a GridItemSpan with a specified currentLineSpan. This will be the horizontal span for an item of a LazyVerticalGrid and the vertical span for a LazyHorizontalGrid.

LazyGridPrefetchStrategy

@ExperimentalFoundationApi
fun LazyGridPrefetchStrategy(nestedPrefetchItemCount: Int = 2): LazyGridPrefetchStrategy

Creates an instance of the default LazyGridPrefetchStrategy, allowing for customization of the nested prefetch count.

Parameters
nestedPrefetchItemCount: Int = 2

specifies how many inner items should be prefetched when this LazyGrid is nested inside another LazyLayout. For example, if this is the state for a horizontal LazyGrid nested in a vertical LazyGrid, you might want to set this to the number of items that will be visible when this grid is scrolled into view. If automatic nested prefetch is enabled, this value will be used as the initial count and the strategy will adapt the count automatically.

LazyLayoutScrollScope

fun LazyLayoutScrollScope(state: LazyGridState, scrollScope: ScrollScope): LazyLayoutScrollScope

An implementation of LazyLayoutScrollScope that can be used with LazyGrids.

import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyGridState
import androidx.compose.foundation.lazy.grid.LazyHorizontalGrid
import androidx.compose.foundation.lazy.grid.LazyLayoutScrollScope
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.foundation.lazy.layout.LazyLayoutScrollScope
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

suspend fun LazyGridState.customScroll(block: suspend LazyLayoutScrollScope.() -> Unit) =
    scroll {
        block.invoke(LazyLayoutScrollScope(this@customScroll, this))
    }

val itemsList = (0..100).toList()
val state = rememberLazyGridState()
val scope = rememberCoroutineScope()

Column(Modifier.verticalScroll(rememberScrollState())) {
    Button(
        onClick = {
            scope.launch {
                state.customScroll {
                    snapToItem(40, 0) // teleport to item 40
                    val distance = calculateDistanceTo(50).toFloat()
                    var previousValue = 0f
                    androidx.compose.animation.core.animate(
                        0f,
                        distance,
                        animationSpec = tween(5_000),
                    ) { currentValue, _ ->
                        previousValue += scrollBy(currentValue - previousValue)
                    }
                }
            }
        }
    ) {
        Text("Scroll To Item 50")
    }
    LazyHorizontalGrid(
        state = state,
        rows = GridCells.Fixed(3),
        modifier = Modifier.height(600.dp).fillMaxWidth(),
    ) {
        items(itemsList) {
            Box(Modifier.padding(2.dp).background(Color.Red).size(45.dp)) {
                Text(it.toString())
            }
        }
    }
}
Parameters
state: LazyGridState

The LazyGridState associated with the layout where this custom scroll should be performed.

scrollScope: ScrollScope

The base ScrollScope where the scroll session was created.

Returns
LazyLayoutScrollScope

An implementation of LazyLayoutScrollScope that works with LazyHorizontalGrid and LazyVerticalGrid.

Extension functions

LazyGridScope.items

inline fun <T : Any?> LazyGridScope.items(
    items: Array<T>,
    noinline key: ((item) -> Any)? = null,
    noinline span: (LazyGridItemSpanScope.(item) -> GridItemSpan)? = null,
    noinline contentType: (item) -> Any? = { null },
    crossinline itemContent: @Composable LazyGridItemScope.(item) -> Unit
): Unit

Adds an array of items.

Parameters
items: Array<T>

the data array

noinline key: ((item) -> Any)? = null

a factory of stable and unique keys representing the item. Using the same key for multiple items in the grid is not allowed. Type of the key should be saveable via Bundle on Android. If null is passed the position in the grid will represent the key. When you specify the key the scroll position will be maintained based on the key, which means if you add/remove items before the current visible item the item with the given key will be kept as the first visible one.This can be overridden by calling LazyGridState.requestScrollToItem.

noinline span: (LazyGridItemSpanScope.(item) -> GridItemSpan)? = null

define custom spans for the items. Default is 1x1. It is good practice to leave it null when this matches the intended behavior, as providing a custom implementation impacts performance

noinline contentType: (item) -> Any? = { null }

a factory of the content types for the item. The item compositions of the same type could be reused more efficiently. Note that null is a valid type and items of such type will be considered compatible.

crossinline itemContent: @Composable LazyGridItemScope.(item) -> Unit

the content displayed by a single item

LazyGridScope.items

inline fun <T : Any?> LazyGridScope.items(
    items: List<T>,
    noinline key: ((item) -> Any)? = null,
    noinline span: (LazyGridItemSpanScope.(item) -> GridItemSpan)? = null,
    noinline contentType: (item) -> Any? = { null },
    crossinline itemContent: @Composable LazyGridItemScope.(item) -> Unit
): Unit

Adds a list of items.

Parameters
items: List<T>

the data list

noinline key: ((item) -> Any)? = null

a factory of stable and unique keys representing the item. Using the same key for multiple items in the grid is not allowed. Type of the key should be saveable via Bundle on Android. If null is passed the position in the grid will represent the key. When you specify the key the scroll position will be maintained based on the key, which means if you add/remove items before the current visible item the item with the given key will be kept as the first visible one. This can be overridden by calling LazyGridState.requestScrollToItem.

noinline span: (LazyGridItemSpanScope.(item) -> GridItemSpan)? = null

define custom spans for the items. Default is 1x1. It is good practice to leave it null when this matches the intended behavior, as providing a custom implementation impacts performance

noinline contentType: (item) -> Any? = { null }

a factory of the content types for the item. The item compositions of the same type could be reused more efficiently. Note that null is a valid type and items of such type will be considered compatible.

crossinline itemContent: @Composable LazyGridItemScope.(item) -> Unit

the content displayed by a single item

LazyGridScope.itemsIndexed

inline fun <T : Any?> LazyGridScope.itemsIndexed(
    items: Array<T>,
    noinline key: ((index: Int, item) -> Any)? = null,
    noinline span: (LazyGridItemSpanScope.(index: Int, item) -> GridItemSpan)? = null,
    crossinline contentType: (index: Int, item) -> Any? = { _, _ -> null },
    crossinline itemContent: @Composable LazyGridItemScope.(index: Int, item) -> Unit
): Unit

Adds an array of items where the content of an item is aware of its index.

Parameters
items: Array<T>

the data array

noinline key: ((index: Int, item) -> Any)? = null

a factory of stable and unique keys representing the item. Using the same key for multiple items in the grid is not allowed. Type of the key should be saveable via Bundle on Android. If null is passed the position in the grid will represent the key. When you specify the key the scroll position will be maintained based on the key, which means if you add/remove items before the current visible item the item with the given key will be kept as the first visible one. This can be overridden by calling LazyGridState.requestScrollToItem.

noinline span: (LazyGridItemSpanScope.(index: Int, item) -> GridItemSpan)? = null

define custom spans for the items. Default is 1x1. It is good practice to leave it null when this matches the intended behavior, as providing a custom implementation impacts performance

crossinline contentType: (index: Int, item) -> Any? = { _, _ -> null }

a factory of the content types for the item. The item compositions of the same type could be reused more efficiently. Note that null is a valid type and items of such type will be considered compatible.

crossinline itemContent: @Composable LazyGridItemScope.(index: Int, item) -> Unit

the content displayed by a single item

LazyGridScope.itemsIndexed

inline fun <T : Any?> LazyGridScope.itemsIndexed(
    items: List<T>,
    noinline key: ((index: Int, item) -> Any)? = null,
    noinline span: (LazyGridItemSpanScope.(index: Int, item) -> GridItemSpan)? = null,
    crossinline contentType: (index: Int, item) -> Any? = { _, _ -> null },
    crossinline itemContent: @Composable LazyGridItemScope.(index: Int, item) -> Unit
): Unit

Adds a list of items where the content of an item is aware of its index.

Parameters
items: List<T>

the data list

noinline key: ((index: Int, item) -> Any)? = null

a factory of stable and unique keys representing the item. Using the same key for multiple items in the grid is not allowed. Type of the key should be saveable via Bundle on Android. If null is passed the position in the grid will represent the key. When you specify the key the scroll position will be maintained based on the key, which means if you add/remove items before the current visible item the item with the given key will be kept as the first visible one. This can be overridden by calling LazyGridState.requestScrollToItem.

noinline span: (LazyGridItemSpanScope.(index: Int, item) -> GridItemSpan)? = null

define custom spans for the items. Default is 1x1. It is good practice to leave it null when this matches the intended behavior, as providing a custom implementation impacts performance

crossinline contentType: (index: Int, item) -> Any? = { _, _ -> null }

a factory of the content types for the item. The item compositions of the same type could be reused more efficiently. Note that null is a valid type and items of such type will be considered compatible.

crossinline itemContent: @Composable LazyGridItemScope.(index: Int, item) -> Unit

the content displayed by a single item