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

LazyPagingItemsKt

{% setvar page_path %}androidx/paging/compose/LazyPagingItemsKt.html{% endsetvar %} {% setvar can_switch %}1{% endsetvar %} {% include "reference/_java_switcher2.md" %}

public final class LazyPagingItemsKt


Summary

Public methods

static final @<ERROR CLASS> @NonNull LazyPagingItems<@NonNull T>
<T extends Object> collectAsLazyPagingItems(
    @NonNull Flow<@NonNull PagingData<@NonNull T>> receiver
)

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

static final void
<T extends Object> items(
    @NonNull <ERROR CLASS> receiver,
    @NonNull LazyPagingItems<@NonNull T> lazyPagingItems,
    @NonNull Function2<@NonNull <ERROR CLASS>, @NonNull value, Unit> itemContent
)

Adds the LazyPagingItems and their content to the scope.

static final void
<T extends Object> itemsIndexed(
    @NonNull <ERROR CLASS> receiver,
    @NonNull LazyPagingItems<@NonNull T> lazyPagingItems,
    @NonNull Function3<@NonNull <ERROR CLASS>, @NonNull Integer, @NonNull value, Unit> itemContent
)

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

Public methods

collectAsLazyPagingItems

@<ERROR CLASS>
@NonNull
public static final @<ERROR CLASS> LazyPagingItems<@NonNull T> <T extends Object> collectAsLazyPagingItems(
    @NonNull Flow<@NonNull PagingData<@NonNull T>> receiver
)

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

@NonNull
public static final void <T extends Object> items(
    @NonNull <ERROR CLASS> receiver,
    @NonNull LazyPagingItems<@NonNull T> lazyPagingItems,
    @NonNull Function2<@NonNull <ERROR CLASS>, @NonNull value, Unit> itemContent
)

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
@NonNull LazyPagingItems<@NonNull T> lazyPagingItems

the items received from a Flow of PagingData.

@NonNull Function2<@NonNull <ERROR CLASS>, @NonNull value, Unit> itemContent

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

@NonNull
public static final void <T extends Object> itemsIndexed(
    @NonNull <ERROR CLASS> receiver,
    @NonNull LazyPagingItems<@NonNull T> lazyPagingItems,
    @NonNull Function3<@NonNull <ERROR CLASS>, @NonNull Integer, @NonNull value, Unit> itemContent
)

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
@NonNull LazyPagingItems<@NonNull T> lazyPagingItems

the items received from a Flow of PagingData.

@NonNull Function3<@NonNull <ERROR CLASS>, @NonNull Integer, @NonNull value, Unit> itemContent

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.