Google is committed to advancing racial equity for Black communities. See how.

androidx.paging.compose

Classes

LazyPagingItems

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

Extension functions summary

For kotlinx.coroutines.flow.Flow
LazyPagingItems<T>

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

For LazyListScope
Unit
LazyListScope.items(lazyPagingItems: LazyPagingItems<T>, itemContent: LazyItemScope.(value: T?) -> Unit)

Adds the LazyPagingItems and their content to the scope.

Unit
LazyListScope.itemsIndexed(lazyPagingItems: LazyPagingItems<T>, itemContent: LazyItemScope.(index: Int, value: T?) -> 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

@Composable fun <T : Any> Flow<PagingData<T>>.collectAsLazyPagingItems(): 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> LazyListScope.items(
    lazyPagingItems: LazyPagingItems<T>,
    itemContent: LazyItemScope.(value: T?) -> 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: LazyItemScope.(value: T?) -> 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> LazyListScope.itemsIndexed(
    lazyPagingItems: LazyPagingItems<T>,
    itemContent: LazyItemScope.(index: Int, value: T?) -> 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: LazyItemScope.(index: Int, value: T?) -> 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.