LazyGridScope


Receiver scope which is used by LazyVerticalGrid.

Summary

Public functions

Unit
item(
    key: Any?,
    span: (LazyGridItemSpanScope.() -> GridItemSpan)?,
    contentType: Any?,
    content: @Composable LazyGridItemScope.() -> Unit
)

Adds a single item to the scope.

Cmn
Unit
items(
    count: Int,
    key: ((index: Int) -> Any)?,
    span: (LazyGridItemSpanScope.(index: Int) -> GridItemSpan)?,
    contentType: (index: Int) -> Any,
    itemContent: @Composable LazyGridItemScope.(index: Int) -> Unit
)

Adds a count of items.

Cmn
Unit
stickyHeader(key: Any?, contentType: Any?, content: @Composable LazyGridItemScope.(Int) -> Unit)

Adds a sticky header item, which will remain pinned even when scrolling after it.

Cmn

Extension functions

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

Public functions

item

fun item(
    key: Any? = null,
    span: (LazyGridItemSpanScope.() -> GridItemSpan)? = null,
    contentType: Any? = null,
    content: @Composable LazyGridItemScope.() -> Unit
): Unit

Adds a single item to the scope.

Parameters
key: Any? = null

a stable and unique key 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.

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

the span of the item. Default is 1x1. It is good practice to leave it null when this matches the intended behavior, as providing a custom implementation impacts performance

contentType: Any? = null

the type of the content of this 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.

content: @Composable LazyGridItemScope.() -> Unit

the content of the item

items

fun items(
    count: Int,
    key: ((index: Int) -> Any)? = null,
    span: (LazyGridItemSpanScope.(index: Int) -> GridItemSpan)? = null,
    contentType: (index: Int) -> Any = { null },
    itemContent: @Composable LazyGridItemScope.(index: Int) -> Unit
): Unit

Adds a count of items.

Parameters
count: Int

the items count

key: ((index: Int) -> 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.

span: (LazyGridItemSpanScope.(index: Int) -> 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

contentType: (index: Int) -> 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.

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

the content displayed by a single item

stickyHeader

fun stickyHeader(
    key: Any? = null,
    contentType: Any? = null,
    content: @Composable LazyGridItemScope.(Int) -> Unit
): Unit

Adds a sticky header item, which will remain pinned even when scrolling after it. The header will remain pinned until the next header will take its place. Sticky Headers are full span items, that is, they will occupy LazyGridItemSpanScope.maxLineSpan.

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

val sections = listOf("A", "B", "C", "D", "E", "F", "G")

LazyVerticalGrid(columns = GridCells.Fixed(3), contentPadding = PaddingValues(6.dp)) {
    sections.forEach { section ->
        stickyHeader {
            Text(
                "Section $section",
                Modifier.fillMaxWidth().background(Color.LightGray).padding(8.dp)
            )
        }
        items(10) { Text("Item= $it S=$section", modifier = Modifier.height(64.dp)) }
    }
}
Parameters
key: Any? = null

a stable and unique key representing the item. Using the same key for multiple items in the list is not allowed. Type of the key should be saveable via Bundle on Android. If null is passed the position in the list 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 'requestScrollToItem' on the 'LazyGridState'.

contentType: Any? = null

the type of the content of this 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.

content: @Composable LazyGridItemScope.(Int) -> Unit

the content of the header. The header index is provided, this is the item position within the total set of items in this lazy list (the global index).

Extension functions

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

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

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

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