ItemSnapshotList



Snapshot of data being presented by a androidx.pagingAsyncPagingDataDiffer or a androidx.paging.PagingDataAdapter.

Summary

Public constructors

<T : Any?> ItemSnapshotList(
    placeholdersBefore: @IntRange(from = 0) Int,
    placeholdersAfter: @IntRange(from = 0) Int,
    items: List<T>
)
Cmn

Public functions

open operator T?
get(index: Int)

Returns the item at index, where index includes the position of placeholders.

Cmn

Public properties

List<T>

The presented data, excluding placeholders.

Cmn
@IntRange(from = 0) Int

Number of placeholders after the presented items, 0 if enablePlaceholders is false.

Cmn
@IntRange(from = 0) Int

Number of placeholders before the presented items, 0 if enablePlaceholders is false.

Cmn
open Int

Size of ItemSnapshotList including placeholders.

Cmn

Extension functions

(index: Int) -> Any?
<T : Any> ItemSnapshotList<T>.itemContentType(contentType: ((item) -> Any?)?)

Returns a factory for the content type of the item.

Cmn
(index: Int) -> Any
<T : Any> ItemSnapshotList<T>.itemKey(key: ((item) -> Any)?)

Returns a factory of stable and unique keys representing the item.

Cmn

Inherited functions

From kotlin.collections.AbstractCollection
open operator Boolean
contains(element: T?)
Cmn
open Boolean
containsAll(elements: Collection<T?>)
Cmn
open Boolean
Cmn
open Array<Any?>
Cmn
open Array<T>
<T : Any?> toArray(array: Array<T>)
Cmn
open String
Cmn
From kotlin.collections.AbstractList
open operator Boolean
equals(other: Any?)
Cmn
open Int
Cmn
open Int
indexOf(element: T?)
Cmn
open operator Iterator<T?>
Cmn
open Int
lastIndexOf(element: T?)
Cmn
open ListIterator<T?>
Cmn
open ListIterator<T?>
listIterator(index: Int)
Cmn
open List<T?>
subList(fromIndex: Int, toIndex: Int)
Cmn

Public constructors

ItemSnapshotList

<T : Any?> ItemSnapshotList(
    placeholdersBefore: @IntRange(from = 0) Int,
    placeholdersAfter: @IntRange(from = 0) Int,
    items: List<T>
)

Public functions

get

open operator fun get(index: Int): T?

Returns the item at index, where index includes the position of placeholders. If index points to the position of a placeholder, null is returned.

To get the size using an index excluding placeholders, use List.size on items directly.

Public properties

items

val items: List<T>

The presented data, excluding placeholders.

placeholdersAfter

val placeholdersAfter: @IntRange(from = 0) Int

Number of placeholders after the presented items, 0 if enablePlaceholders is false.

placeholdersBefore

val placeholdersBefore: @IntRange(from = 0) Int

Number of placeholders before the presented items, 0 if enablePlaceholders is false.

size

open val size: Int

Size of ItemSnapshotList including placeholders.

To get the size excluding placeholders, use List.size on items directly.

See also
items

Extension functions

ItemSnapshotList.itemContentType

fun <T : Any> ItemSnapshotList<T>.itemContentType(
    contentType: ((item) -> Any?)? = null
): (index: Int) -> Any?

Returns a factory for the content type of the item.

ContentTypes are generated with the contentType lambda that is passed in. If null is passed in, contentType of all items will default to null. If PagingConfig.enablePlaceholders is true, ItemSnapshotList may return null items. Null items will automatically default to placeholder contentType.

This factory can be applied to Lazy foundations such as LazyGridScope.items or Pagers. Examples:

import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.itemContentType
import androidx.paging.compose.itemKey

val lazyPagingItems = pager.collectAsLazyPagingItems()

LazyVerticalGrid(columns = GridCells.Fixed(2)) {
    items(
        count = lazyPagingItems.itemCount,
        key = lazyPagingItems.itemKey { it },
        contentType = lazyPagingItems.itemContentType { "MyPagingItems" },
    ) { index ->
        val item = lazyPagingItems[index]
        PagingItem(item = item)
    }
}
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.itemContentType
import androidx.paging.compose.itemKey

val lazyPagingItems = pager.collectAsLazyPagingItems()

LazyColumn {
    stickyHeader(key = "Header", contentType = "My Header") {
        Box(
            modifier = Modifier.padding(bottom = 10.dp).background(Color.Red).fillMaxWidth(),
            contentAlignment = Alignment.Center,
        ) {
            Text(text = "Header", fontSize = 32.sp)
        }
    }
    items(
        count = lazyPagingItems.itemCount,
        key = lazyPagingItems.itemKey { it },
        contentType = lazyPagingItems.itemContentType { "MyPagingItems" },
    ) { index ->
        val item = lazyPagingItems[index]
        PagingItem(item = item)
    }
}
Parameters
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.

ItemSnapshotList.itemKey

fun <T : Any> ItemSnapshotList<T>.itemKey(key: ((item) -> Any)? = null): (index: Int) -> Any

Returns a factory of stable and unique keys representing the item.

Keys are generated with the key lambda that is passed in. If null is passed in, keys will default to a placeholder key. If PagingConfig.enablePlaceholders is true, ItemSnapshotList may return null items. Null items will also automatically default to a placeholder key.

This factory can be applied to Lazy foundations such as compose foundation's LazyGridScope or Pagers. Examples:

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.PageSize
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.paging.Pager
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.itemKey

val lazyPagingItems = pager.collectAsLazyPagingItems()
val pagerState = rememberPagerState { lazyPagingItems.itemCount }

HorizontalPager(
    modifier = Modifier.fillMaxSize(),
    state = pagerState,
    pageSize = PageSize.Fixed(200.dp),
    key = lazyPagingItems.itemKey { it },
) { index ->
    val item = lazyPagingItems[index]
    PagingItem(item = item)
}
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.itemContentType
import androidx.paging.compose.itemKey

val lazyPagingItems = pager.collectAsLazyPagingItems()

LazyVerticalGrid(columns = GridCells.Fixed(2)) {
    items(
        count = lazyPagingItems.itemCount,
        key = lazyPagingItems.itemKey { it },
        contentType = lazyPagingItems.itemContentType { "MyPagingItems" },
    ) { index ->
        val item = lazyPagingItems[index]
        PagingItem(item = item)
    }
}
Parameters
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. 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.