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

androidx.paging.compose

{% setvar page_path %}androidx/paging/compose/package-summary.html{% endsetvar %} {% setvar can_switch %}1{% endsetvar %} {% include "reference/_kotlin_switcher2.md" %}

Classes

LazyPagingItems

The class responsible for accessing the data from a Flow of PagingData.

Extension functions summary

@<ERROR CLASS> LazyPagingItems<T>

Collects values from this Flow of PagingData and represents them inside a LazyPagingItems instance.

Unit
<T : Any> <ERROR CLASS>.items(
    lazyPagingItems: LazyPagingItems<T>,
    itemContent: <ERROR CLASS>.(value?) -> Unit
)

Adds the LazyPagingItems and their content to the scope.

Unit
<T : Any> <ERROR CLASS>.itemsIndexed(
    lazyPagingItems: LazyPagingItems<T>,
    itemContent: <ERROR CLASS>.(Int, value?) -> Unit
)

Adds the LazyPagingItems and their content to the scope where the content of an item is aware of its local index.

Extension functions

collectAsLazyPagingItems

@<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)
            )
        }
    }
}

items

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 Flow of PagingData.

itemContent: <ERROR CLASS>.(value?) -> Unit

the content displayed by a single item. In case the item is null, the itemContent method should handle the logic of displaying a placeholder instead of the main content displayed by an item which is not null.

itemsIndexed

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 Flow of PagingData.

itemContent: <ERROR CLASS>.(Int, value?) -> Unit

the content displayed by a single item. In case the item is null, the itemContent method should handle the logic of displaying a placeholder instead of the main content displayed by an item which is not null.