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

LazyListScope

@LazyScopeMarker
public interface LazyListScope


Receiver scope which is used by LazyColumn and LazyRow.

Summary

Public methods

default void
item(
    Object key,
    Object contentType,
    @Composable @ExtensionFunctionType @NonNull Function1<@NonNull LazyItemScopeUnit> content
)

Adds a single item.

default void
items(
    int count,
    Function1<@NonNull Integer, @NonNull Object> key,
    @NonNull Function1<@NonNull IntegerObject> contentType,
    @Composable @ExtensionFunctionType @NonNull Function2<@NonNull LazyItemScope, @NonNull IntegerUnit> itemContent
)

Adds a count of items.

abstract void
@ExperimentalFoundationApi
stickyHeader(
    Object key,
    Object contentType,
    @Composable @ExtensionFunctionType @NonNull Function1<@NonNull LazyItemScopeUnit> content
)

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

Extension functions

default final void
<T extends Object> LazyDslKt.items(
    @NonNull LazyListScope receiver,
    @NonNull T[] items,
    Function1<@NonNull item, @NonNull Object> key,
    @NonNull Function1<@NonNull item, Object> contentType,
    @Composable @ExtensionFunctionType @NonNull Function2<@NonNull LazyItemScope, @NonNull item, Unit> itemContent
)

Adds an array of items.

default final void
<T extends Object> LazyDslKt.items(
    @NonNull LazyListScope receiver,
    @NonNull List<@NonNull T> items,
    Function1<@NonNull item, @NonNull Object> key,
    @NonNull Function1<@NonNull item, Object> contentType,
    @Composable @ExtensionFunctionType @NonNull Function2<@NonNull LazyItemScope, @NonNull item, Unit> itemContent
)

Adds a list of items.

default final void
<T extends Object> LazyDslKt.itemsIndexed(
    @NonNull LazyListScope receiver,
    @NonNull T[] items,
    Function2<@NonNull Integer, @NonNull item, @NonNull Object> key,
    @NonNull Function2<@NonNull Integer, @NonNull item, Object> contentType,
    @Composable @ExtensionFunctionType @NonNull Function3<@NonNull LazyItemScope, @NonNull Integer, @NonNull item, Unit> itemContent
)

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

default final void
<T extends Object> LazyDslKt.itemsIndexed(
    @NonNull LazyListScope receiver,
    @NonNull List<@NonNull T> items,
    Function2<@NonNull Integer, @NonNull item, @NonNull Object> key,
    @NonNull Function2<@NonNull Integer, @NonNull item, Object> contentType,
    @Composable @ExtensionFunctionType @NonNull Function3<@NonNull LazyItemScope, @NonNull Integer, @NonNull item, Unit> itemContent
)

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

Public methods

item

default void item(
    Object key,
    Object contentType,
    @Composable @ExtensionFunctionType @NonNull Function1<@NonNull LazyItemScopeUnit> content
)

Adds a single item.

Parameters
Object key

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.

Object contentType

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.

@Composable @ExtensionFunctionType @NonNull Function1<@NonNull LazyItemScopeUnit> content

the content of the item

items

default void items(
    int count,
    Function1<@NonNull Integer, @NonNull Object> key,
    @NonNull Function1<@NonNull IntegerObject> contentType,
    @Composable @ExtensionFunctionType @NonNull Function2<@NonNull LazyItemScope, @NonNull IntegerUnit> itemContent
)

Adds a count of items.

Parameters
int count

the items count

Function1<@NonNull Integer, @NonNull Object> key

a factory of stable and unique keys 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.

@NonNull Function1<@NonNull IntegerObject> contentType

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.

@Composable @ExtensionFunctionType @NonNull Function2<@NonNull LazyItemScope, @NonNull IntegerUnit> itemContent

the content displayed by a single item

stickyHeader

@ExperimentalFoundationApi
abstract void stickyHeader(
    Object key,
    Object contentType,
    @Composable @ExtensionFunctionType @NonNull Function1<@NonNull LazyItemScopeUnit> content
)

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.

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
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")

LazyColumn(reverseLayout = true, contentPadding = PaddingValues(6.dp)) {
    sections.forEach { section ->
        stickyHeader {
            Text(
                "Section $section",
                Modifier.fillMaxWidth().background(Color.LightGray).padding(8.dp)
            )
        }
        items(10) {
            Text("Item $it from the section $section")
        }
    }
}
Parameters
Object key

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.

Object contentType

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.

@Composable @ExtensionFunctionType @NonNull Function1<@NonNull LazyItemScopeUnit> content

the content of the header

Extension functions

LazyDslKt.items

default final void <T extends Object> LazyDslKt.items(
    @NonNull LazyListScope receiver,
    @NonNull T[] items,
    Function1<@NonNull item, @NonNull Object> key,
    @NonNull Function1<@NonNull item, Object> contentType,
    @Composable @ExtensionFunctionType @NonNull Function2<@NonNull LazyItemScope, @NonNull item, Unit> itemContent
)

Adds an array of items.

Parameters
@NonNull T[] items

the data array

Function1<@NonNull item, @NonNull Object> key

a factory of stable and unique keys 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.

@NonNull Function1<@NonNull item, Object> contentType

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.

@Composable @ExtensionFunctionType @NonNull Function2<@NonNull LazyItemScope, @NonNull item, Unit> itemContent

the content displayed by a single item

LazyDslKt.items

default final void <T extends Object> LazyDslKt.items(
    @NonNull LazyListScope receiver,
    @NonNull List<@NonNull T> items,
    Function1<@NonNull item, @NonNull Object> key,
    @NonNull Function1<@NonNull item, Object> contentType,
    @Composable @ExtensionFunctionType @NonNull Function2<@NonNull LazyItemScope, @NonNull item, Unit> itemContent
)

Adds a list of items.

Parameters
@NonNull List<@NonNull T> items

the data list

Function1<@NonNull item, @NonNull Object> key

a factory of stable and unique keys 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.

@NonNull Function1<@NonNull item, Object> contentType

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.

@Composable @ExtensionFunctionType @NonNull Function2<@NonNull LazyItemScope, @NonNull item, Unit> itemContent

the content displayed by a single item

LazyDslKt.itemsIndexed

default final void <T extends Object> LazyDslKt.itemsIndexed(
    @NonNull LazyListScope receiver,
    @NonNull T[] items,
    Function2<@NonNull Integer, @NonNull item, @NonNull Object> key,
    @NonNull Function2<@NonNull Integer, @NonNull item, Object> contentType,
    @Composable @ExtensionFunctionType @NonNull Function3<@NonNull LazyItemScope, @NonNull Integer, @NonNull item, Unit> itemContent
)

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

Parameters
@NonNull T[] items

the data array

Function2<@NonNull Integer, @NonNull item, @NonNull Object> key

a factory of stable and unique keys 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.

@NonNull Function2<@NonNull Integer, @NonNull item, Object> contentType

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.

@Composable @ExtensionFunctionType @NonNull Function3<@NonNull LazyItemScope, @NonNull Integer, @NonNull item, Unit> itemContent

the content displayed by a single item

LazyDslKt.itemsIndexed

default final void <T extends Object> LazyDslKt.itemsIndexed(
    @NonNull LazyListScope receiver,
    @NonNull List<@NonNull T> items,
    Function2<@NonNull Integer, @NonNull item, @NonNull Object> key,
    @NonNull Function2<@NonNull Integer, @NonNull item, Object> contentType,
    @Composable @ExtensionFunctionType @NonNull Function3<@NonNull LazyItemScope, @NonNull Integer, @NonNull item, Unit> itemContent
)

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

Parameters
@NonNull List<@NonNull T> items

the data list

Function2<@NonNull Integer, @NonNull item, @NonNull Object> key

a factory of stable and unique keys 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.

@NonNull Function2<@NonNull Integer, @NonNull item, Object> contentType

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.

@Composable @ExtensionFunctionType @NonNull Function3<@NonNull LazyItemScope, @NonNull Integer, @NonNull item, Unit> itemContent

the content displayed by a single item