{% setvar book_path %}/reference/kotlin/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}
LazyPagingItems |
The class responsible for accessing the data from a |
@<ERROR CLASS> LazyPagingItems<T> |
<T : Any> Flow<PagingData<T>>.collectAsLazyPagingItems() Collects values from this |
Unit |
<T : Any> <ERROR CLASS>.items( Adds the |
Unit |
<T : Any> <ERROR CLASS>.itemsIndexed( Adds the |
@<ERROR CLASS>
fun <T : Any> Flow<PagingData<T>>.collectAsLazyPagingItems(): @<ERROR CLASS> LazyPagingItems<T>
Collects values from this Flow
of PagingData
and represents them inside a LazyPagingItems
instance. The LazyPagingItems
instance can be used by the items
and itemsIndexed
methods from LazyListScope in order to display the data obtained from a Flow
of PagingData
.
import androidx.compose.foundation.Text import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.wrapContentWidth import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.material.CircularProgressIndicator import androidx.compose.runtime.remember import androidx.paging.Pager import androidx.paging.PagingConfig import androidx.paging.compose.collectAsLazyPagingItems import androidx.paging.compose.itemsIndexed val myBackend = remember { MyBackend() } val pager = remember { Pager( PagingConfig( pageSize = myBackend.DataBatchSize, enablePlaceholders = true, maxSize = 200 ) ) { myBackend.getAllData() } } val lazyPagingItems = pager.flow.collectAsLazyPagingItems() LazyColumn { if (lazyPagingItems.loadState.refresh == LoadState.Loading) { item { Text( text = "Waiting for items to load from the backend", modifier = Modifier.fillMaxWidth() .wrapContentWidth(Alignment.CenterHorizontally) ) } } itemsIndexed(lazyPagingItems) { index, item -> Text("Index=$index: $item", fontSize = 20.sp) } if (lazyPagingItems.loadState.append == LoadState.Loading) { item { CircularProgressIndicator( modifier = Modifier.fillMaxWidth() .wrapContentWidth(Alignment.CenterHorizontally) ) } } }
fun <T : Any> <ERROR CLASS>.items(
lazyPagingItems: LazyPagingItems<T>,
itemContent: <ERROR CLASS>.(value?) -> Unit
): Unit
Adds the LazyPagingItems
and their content to the scope. The range from 0 (inclusive) to LazyPagingItems.itemCount
(inclusive) always represents the full range of presentable items, because every event from PagingDataDiffer
will trigger a recomposition.
import androidx.compose.foundation.Text import androidx.compose.foundation.lazy.LazyColumn import androidx.paging.compose.collectAsLazyPagingItems import androidx.paging.compose.items val lazyPagingItems = flow.collectAsLazyPagingItems() LazyColumn { items(lazyPagingItems) { Text("Item is $it") } }
Parameters | |
---|---|
lazyPagingItems: LazyPagingItems<T> |
the items received from a |
itemContent: <ERROR CLASS>.(value?) -> Unit |
the content displayed by a single item. In case the item is |
fun <T : Any> <ERROR CLASS>.itemsIndexed(
lazyPagingItems: LazyPagingItems<T>,
itemContent: <ERROR CLASS>.(Int, value?) -> Unit
): Unit
Adds the LazyPagingItems
and their content to the scope where the content of an item is aware of its local index. The range from 0 (inclusive) to LazyPagingItems.itemCount
(inclusive) always represents the full range of presentable items, because every event from PagingDataDiffer
will trigger a recomposition.
import androidx.compose.foundation.Text import androidx.compose.foundation.lazy.LazyColumn import androidx.paging.compose.collectAsLazyPagingItems import androidx.paging.compose.itemsIndexed val lazyPagingItems = flow.collectAsLazyPagingItems() LazyColumn { itemsIndexed(lazyPagingItems) { index, item -> Text("Item at index $index is $item") } }
Parameters | |
---|---|
lazyPagingItems: LazyPagingItems<T> |
the items received from a |
itemContent: <ERROR CLASS>.(Int, value?) -> Unit |
the content displayed by a single item. In case the item is |