androidx.compose.foundation.lazy
In this page, you'll find documentation for types, properties, and functions available in the androidx.compose.foundation.lazy package such as LazyColumn, LazyRow, and rememberLazyListState.
If you're looking for guidance instead, check out the Lists in Compose guide.
Interfaces
LazyItemScope |
Receiver scope being used by the item content parameter of LazyColumn/Row. |
Cmn
|
LazyListItemInfo |
Contains useful information about an individual item in lazy lists like |
Cmn
|
LazyListLayoutInfo |
Contains useful information about the currently displayed layout state of lazy lists like |
Cmn
|
LazyListPrefetchResultScope |
A scope for |
Cmn
|
LazyListPrefetchScope |
Scope for callbacks in |
Cmn
|
LazyListPrefetchStrategy |
Implementations of this interface control which indices of a LazyList should be prefetched (precomposed and premeasured during idle time) as the user interacts with it. |
Cmn
|
LazyListScope |
Receiver scope which is used by |
Cmn
|
Classes
LazyListState |
A state object that can be hoisted to control and observe scrolling. |
Cmn
|
Annotations
LazyScopeMarker |
DSL marker used to distinguish between lazy layout scope and the item scope. |
Cmn
|
Top-level functions summary
Unit |
@ComposableThe vertically scrolling list that only composes and lays out the currently visible items. |
Cmn
|
LazyLayoutScrollScope |
LazyLayoutScrollScope(state: LazyListState, scrollScope: ScrollScope)An implementation of |
Cmn
|
LazyListPrefetchStrategy |
@ExperimentalFoundationApiCreates an instance of the default |
Cmn
|
Unit |
@ComposableThe horizontally scrolling list that only composes and lays out the currently visible items. |
Cmn
|
LazyListState |
@ComposableCreates a |
Cmn
|
LazyListState |
@ExperimentalFoundationApiCreates a |
Cmn
|
LazyListState |
@ExperimentalFoundationApiCreates a |
Cmn
|
Extension functions summary
inline Unit |
<T : Any?> LazyListScope.items(Adds an array of items. |
Cmn
|
inline Unit |
<T : Any?> LazyListScope.items(Adds a list of items. |
Cmn
|
inline Unit |
<T : Any?> LazyListScope.itemsIndexed(Adds an array of items where the content of an item is aware of its index. |
Cmn
|
inline Unit |
<T : Any?> LazyListScope.itemsIndexed(Adds a list of items where the content of an item is aware of its index. |
Cmn
|
Top-level functions
LazyColumn
@Composable
fun LazyColumn(
modifier: Modifier = Modifier,
state: LazyListState = rememberLazyListState(),
contentPadding: PaddingValues = PaddingValues(0.dp),
reverseLayout: Boolean = false,
verticalArrangement: Arrangement.Vertical = if (!reverseLayout) Arrangement.Top else Arrangement.Bottom,
horizontalAlignment: Alignment.Horizontal = Alignment.Start,
flingBehavior: FlingBehavior = ScrollableDefaults.flingBehavior(),
userScrollEnabled: Boolean = true,
overscrollEffect: OverscrollEffect? = rememberOverscrollEffect(),
content: LazyListScope.() -> Unit
): Unit
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.layout.Column 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 | |
|---|---|
modifier: Modifier = Modifier |
the modifier to apply to this layout. |
state: LazyListState = rememberLazyListState() |
the state object to be used to control or observe the list's state. |
contentPadding: PaddingValues = PaddingValues(0.dp) |
a padding around the whole content. This will add padding for the. content after it has been clipped, which is not possible via |
reverseLayout: Boolean = false |
reverse the direction of scrolling and layout. When |
verticalArrangement: Arrangement.Vertical = if (!reverseLayout) Arrangement.Top else Arrangement.Bottom |
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. |
horizontalAlignment: Alignment.Horizontal = Alignment.Start |
the horizontal alignment applied to the items. |
flingBehavior: FlingBehavior = ScrollableDefaults.flingBehavior() |
logic describing fling behavior. |
userScrollEnabled: Boolean = true |
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 |
overscrollEffect: OverscrollEffect? = rememberOverscrollEffect() |
the |
content: LazyListScope.() -> Unit |
a block which describes the content. Inside this block you can use methods like |
LazyLayoutScrollScope
fun LazyLayoutScrollScope(state: LazyListState, scrollScope: ScrollScope): LazyLayoutScrollScope
An implementation of LazyLayoutScrollScope that can be used with LazyLists. Please refer to the sample to learn how to use this API.
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.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.lazy.LazyLayoutScrollScope import androidx.compose.foundation.lazy.LazyListState import androidx.compose.foundation.lazy.LazyRow import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.layout.LazyLayoutScrollScope import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.verticalScroll import androidx.compose.material.Button import androidx.compose.material.Text import androidx.compose.runtime.State 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 LazyListState.customScroll(block: suspend LazyLayoutScrollScope.() -> Unit) = scroll { block.invoke(LazyLayoutScrollScope(this@customScroll, this)) } val itemsList = (0..100).toList() val state = rememberLazyListState() 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") } LazyRow(state = state) { items(itemsList) { Box(Modifier.padding(2.dp).background(Color.Red).size(45.dp)) { Text(it.toString()) } } } }
| Parameters | |
|---|---|
state: LazyListState |
The |
scrollScope: ScrollScope |
The base |
| Returns | |
|---|---|
LazyLayoutScrollScope |
An implementation of |
LazyListPrefetchStrategy
@ExperimentalFoundationApi
fun LazyListPrefetchStrategy(nestedPrefetchItemCount: Int = 2): LazyListPrefetchStrategy
Creates an instance of the default LazyListPrefetchStrategy, allowing for customization of the nested prefetch count.
| Parameters | |
|---|---|
nestedPrefetchItemCount: Int = 2 |
specifies how many inner items should be prefetched when this LazyList is nested inside another LazyLayout. For example, if this is the state for a horizontal LazyList nested in a vertical LazyList, you might want to set this to the number of items that will be visible when this list 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. |
LazyRow
@Composable
fun LazyRow(
modifier: Modifier = Modifier,
state: LazyListState = rememberLazyListState(),
contentPadding: PaddingValues = PaddingValues(0.dp),
reverseLayout: Boolean = false,
horizontalArrangement: Arrangement.Horizontal = if (!reverseLayout) Arrangement.Start else Arrangement.End,
verticalAlignment: Alignment.Vertical = Alignment.Top,
flingBehavior: FlingBehavior = ScrollableDefaults.flingBehavior(),
userScrollEnabled: Boolean = true,
overscrollEffect: OverscrollEffect? = rememberOverscrollEffect(),
content: LazyListScope.() -> Unit
): Unit
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 | |
|---|---|
modifier: Modifier = Modifier |
the modifier to apply to this layout |
state: LazyListState = rememberLazyListState() |
the state object to be used to control or observe the list's state |
contentPadding: PaddingValues = PaddingValues(0.dp) |
a padding around the whole content. This will add padding for the content after it has been clipped, which is not possible via |
reverseLayout: Boolean = false |
reverse the direction of scrolling and layout. When |
horizontalArrangement: Arrangement.Horizontal = if (!reverseLayout) Arrangement.Start else Arrangement.End |
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. |
verticalAlignment: Alignment.Vertical = Alignment.Top |
the vertical alignment applied to the items |
flingBehavior: FlingBehavior = ScrollableDefaults.flingBehavior() |
logic describing fling behavior. |
userScrollEnabled: Boolean = true |
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. |
overscrollEffect: OverscrollEffect? = rememberOverscrollEffect() |
the |
content: LazyListScope.() -> Unit |
a block which describes the content. Inside this block you can use methods like |
rememberLazyListState
@Composable
fun rememberLazyListState(
initialFirstVisibleItemIndex: Int = 0,
initialFirstVisibleItemScrollOffset: Int = 0
): LazyListState
Creates a LazyListState that is remembered across compositions.
Changes to the provided initial values will not result in the state being recreated or changed in any way if it has already been created.
| Parameters | |
|---|---|
initialFirstVisibleItemIndex: Int = 0 |
the initial value for |
initialFirstVisibleItemScrollOffset: Int = 0 |
the initial value for |
rememberLazyListState
@ExperimentalFoundationApi
@Composable
fun rememberLazyListState(
cacheWindow: LazyLayoutCacheWindow,
initialFirstVisibleItemIndex: Int = 0,
initialFirstVisibleItemScrollOffset: Int = 0
): LazyListState
Creates a LazyListState that is remembered across compositions.
Changes to the provided initial values will not result in the state being recreated or changed in any way if it has already been created.
| Parameters | |
|---|---|
cacheWindow: LazyLayoutCacheWindow |
specifies the size of the ahead and behind window to be used as per |
initialFirstVisibleItemIndex: Int = 0 |
the initial value for |
initialFirstVisibleItemScrollOffset: Int = 0 |
the initial value for |
rememberLazyListState
@ExperimentalFoundationApi
@Composable
fun rememberLazyListState(
initialFirstVisibleItemIndex: Int = 0,
initialFirstVisibleItemScrollOffset: Int = 0,
prefetchStrategy: LazyListPrefetchStrategy = remember { LazyListPrefetchStrategy() }
): LazyListState
Creates a LazyListState that is remembered across compositions.
Changes to the provided initial values will not result in the state being recreated or changed in any way if it has already been created.
| Parameters | |
|---|---|
initialFirstVisibleItemIndex: Int = 0 |
the initial value for |
initialFirstVisibleItemScrollOffset: Int = 0 |
the initial value for |
prefetchStrategy: LazyListPrefetchStrategy = remember { LazyListPrefetchStrategy() } |
the |
Extension functions
items
inline fun <T : Any?> LazyListScope.items(
items: Array<T>,
noinline key: ((item) -> Any)? = null,
noinline contentType: (item) -> Any? = { null },
crossinline itemContent: @Composable LazyItemScope.(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 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 'LazyListState'. |
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 LazyItemScope.(item) -> Unit |
the content displayed by a single item |
items
inline fun <T : Any?> LazyListScope.items(
items: List<T>,
noinline key: ((item) -> Any)? = null,
noinline contentType: (item) -> Any? = { null },
crossinline itemContent: @Composable LazyItemScope.(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 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 'LazyListState'. |
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 LazyItemScope.(item) -> Unit |
the content displayed by a single item |
itemsIndexed
inline fun <T : Any?> LazyListScope.itemsIndexed(
items: Array<T>,
noinline key: ((index: Int, item) -> Any)? = null,
crossinline contentType: (index: Int, item) -> Any? = { _, _ -> null },
crossinline itemContent: @Composable LazyItemScope.(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 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 'LazyListState'. |
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 LazyItemScope.(index: Int, item) -> Unit |
the content displayed by a single item |
itemsIndexed
inline fun <T : Any?> LazyListScope.itemsIndexed(
items: List<T>,
noinline key: ((index: Int, item) -> Any)? = null,
crossinline contentType: (index: Int, item) -> Any? = { _, _ -> null },
crossinline itemContent: @Composable LazyItemScope.(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 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 'LazyListState'. |
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 LazyItemScope.(index: Int, item) -> Unit |
the content displayed by a single item |