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

LazyDslKt

public final class LazyDslKt


Summary

Public methods

static final void
@Composable
LazyColumn(
    @NonNull Modifier modifier,
    @NonNull LazyListState state,
    @NonNull PaddingValues contentPadding,
    boolean reverseLayout,
    @NonNull Arrangement.Vertical verticalArrangement,
    @NonNull Alignment.Horizontal horizontalAlignment,
    @NonNull FlingBehavior flingBehavior,
    boolean userScrollEnabled,
    @ExtensionFunctionType @NonNull Function1<@NonNull LazyListScopeUnit> content
)

The vertically scrolling list that only composes and lays out the currently visible items.

static final void
@Composable
LazyRow(
    @NonNull Modifier modifier,
    @NonNull LazyListState state,
    @NonNull PaddingValues contentPadding,
    boolean reverseLayout,
    @NonNull Arrangement.Horizontal horizontalArrangement,
    @NonNull Alignment.Vertical verticalAlignment,
    @NonNull FlingBehavior flingBehavior,
    boolean userScrollEnabled,
    @ExtensionFunctionType @NonNull Function1<@NonNull LazyListScopeUnit> content
)

The horizontally scrolling list that only composes and lays out the currently visible items.

static final void
<T extends Object> 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.

static final void
<T extends Object> 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.

static final void
<T extends Object> 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.

static final void
<T extends Object> 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

LazyColumn

@Composable
public static final void LazyColumn(
    @NonNull Modifier modifier,
    @NonNull LazyListState state,
    @NonNull PaddingValues contentPadding,
    boolean reverseLayout,
    @NonNull Arrangement.Vertical verticalArrangement,
    @NonNull Alignment.Horizontal horizontalAlignment,
    @NonNull FlingBehavior flingBehavior,
    boolean userScrollEnabled,
    @ExtensionFunctionType @NonNull Function1<@NonNull LazyListScopeUnit> content
)

The vertically scrolling list that only composes and lays out the currently visible items. The content block defines a DSL which allows you to emit items of different types. For example you can use LazyListScope.item to add a single item and LazyListScope.items to add a list of items.

import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material.Text

val itemsList = (0..5).toList()
val itemsIndexedList = listOf("A", "B", "C")

LazyColumn {
    items(itemsList) {
        Text("Item is $it")
    }

    item {
        Text("Single item")
    }

    itemsIndexed(itemsIndexedList) { index, item ->
        Text("Item at index $index is $item")
    }
}
Parameters
@NonNull Modifier modifier

the modifier to apply to this layout.

@NonNull LazyListState state

the state object to be used to control or observe the list's state.

@NonNull PaddingValues contentPadding

a padding around the whole content. This will add padding for the. content after it has been clipped, which is not possible via modifier param. You can use it to add a padding before the first item or after the last one. If you want to add a spacing between each item use verticalArrangement.

boolean reverseLayout

reverse the direction of scrolling and layout. When true, items are laid out in the reverse order and LazyListState.firstVisibleItemIndex == 0 means that column is scrolled to the bottom. Note that reverseLayout does not change the behavior of verticalArrangement, e.g. with Arrangement.Top (top) 123### (bottom) becomes (top) 321### (bottom).

@NonNull Arrangement.Vertical verticalArrangement

The vertical arrangement of the layout's children. This allows to add a spacing between items and specify the arrangement of the items when we have not enough of them to fill the whole minimum size.

@NonNull Alignment.Horizontal horizontalAlignment

the horizontal alignment applied to the items.

@NonNull FlingBehavior flingBehavior

logic describing fling behavior.

boolean userScrollEnabled

whether the scrolling via the user gestures or accessibility actions is allowed. You can still scroll programmatically using the state even when it is disabled

@ExtensionFunctionType @NonNull Function1<@NonNull LazyListScopeUnit> content

a block which describes the content. Inside this block you can use methods like LazyListScope.item to add a single item or LazyListScope.items to add a list of items.

LazyRow

@Composable
public static final void LazyRow(
    @NonNull Modifier modifier,
    @NonNull LazyListState state,
    @NonNull PaddingValues contentPadding,
    boolean reverseLayout,
    @NonNull Arrangement.Horizontal horizontalArrangement,
    @NonNull Alignment.Vertical verticalAlignment,
    @NonNull FlingBehavior flingBehavior,
    boolean userScrollEnabled,
    @ExtensionFunctionType @NonNull Function1<@NonNull LazyListScopeUnit> content
)

The horizontally scrolling list that only composes and lays out the currently visible items. The content block defines a DSL which allows you to emit items of different types. For example you can use LazyListScope.item to add a single item and LazyListScope.items to add a list of items.

import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material.Text

val itemsList = (0..5).toList()
val itemsIndexedList = listOf("A", "B", "C")

LazyRow {
    items(itemsList) {
        Text("Item is $it")
    }

    item {
        Text("Single item")
    }

    itemsIndexed(itemsIndexedList) { index, item ->
        Text("Item at index $index is $item")
    }
}
Parameters
@NonNull Modifier modifier

the modifier to apply to this layout

@NonNull LazyListState state

the state object to be used to control or observe the list's state

@NonNull PaddingValues contentPadding

a padding around the whole content. This will add padding for the content after it has been clipped, which is not possible via modifier param. You can use it to add a padding before the first item or after the last one. If you want to add a spacing between each item use horizontalArrangement.

boolean reverseLayout

reverse the direction of scrolling and layout. When true, items are laid out in the reverse order and LazyListState.firstVisibleItemIndex == 0 means that row is scrolled to the end. Note that reverseLayout does not change the behavior of horizontalArrangement, e.g. with 123### becomes 321###.

@NonNull Arrangement.Horizontal horizontalArrangement

The horizontal arrangement of the layout's children. This allows to add a spacing between items and specify the arrangement of the items when we have not enough of them to fill the whole minimum size.

@NonNull Alignment.Vertical verticalAlignment

the vertical alignment applied to the items

@NonNull FlingBehavior flingBehavior

logic describing fling behavior.

boolean userScrollEnabled

whether the scrolling via the user gestures or accessibility actions is allowed. You can still scroll programmatically using the state even when it is disabled.

@ExtensionFunctionType @NonNull Function1<@NonNull LazyListScopeUnit> content

a block which describes the content. Inside this block you can use methods like LazyListScope.item to add a single item or LazyListScope.items to add a list of items.

items

public static final void <T extends Object> 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

items

public static final void <T extends Object> 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

itemsIndexed

public static final void <T extends Object> 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

itemsIndexed

public static final void <T extends Object> 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