LazyLayoutItemProvider

Interface

Common
interface LazyLayoutItemProvider

Provides all the needed info about the items which could be later composed and displayed as children or LazyLayout. The number of virtual items is limited by LazyLayoutItemProvider.itemCount. For an efficient implementation this should be an immutable representation of the underlying data/items, ideally changes to your data source/structure should re-create this representation, instead of changing state values read inside this implementation. See LazyLayout for additional context.

Properties

Common
@get:IntRange(from = 0) val itemCount: Int

The total number of items in the lazy layout (visible or not).

Functions

@Composable fun Item(@IntRange(from = 0) index: Int, key: Any)

The item for the given index and key. Indices are a core concept on LazyLayouts and should always be supported, the key is an additional concept that can be introduced per item and is used to guaranteed the uniqueness of a given item in the Lazy Layout implementation. For instance, in lists, keys are used to keep an item's scroll position in case of dataset changes. Therefore, the key provided here will depend if your LazyLayout implementation supports this concept or not.

Custom keys are provided in the getKey function. If a key concept is not needed by your LazyLayout implementation (getKey is not overridden), the internal implementation will simply use getDefaultLazyLayoutKey to implement a key based on an item's index.

It is the responsibility of the implementation to ensure consistency between keys and indices if custom keys are provided, since both the caller and the implementer of this function are the same, a contract can be established to ensure the consistency.

Parameters

indexthe index of the item in the list
keyThe key of the item as described above.
fun getContentType(@IntRange(from = 0) index: Int): Any?

Returns the content type for the item on this index. It is used to improve the item compositions reusing efficiency. Note that null is a valid type and items of such type will be considered compatible.

Parameters

indexthe index of an item in the layout.

Returns

The content type mapped from index.
fun getKey(@IntRange(from = 0) index: Int): Any

Returns the key for the item on this index.

Parameters

indexthe index of an item in the layout.

Returns

The key mapped from index.
fun getIndex(key: Any): Int

Get index for given key. The index is not guaranteed to be known for all keys in layout for optimization purposes, but must be present for elements in current viewport. If the key is not present in the layout or is not known, return -1.

Parameters

keythe key of an item in the layout.

Returns

The index mapped from key if it is present in the layout, otherwise -1.

Code Examples

LazyLayoutItemProviderSample

@Composable
fun LazyLayoutItemProviderSample() {
    class CustomItemProvider(val data: List<String>) : LazyLayoutItemProvider {
        override val itemCount: Int
            get() = data.size
        @Composable
        override fun Item(index: Int, key: Any) {
            Box(
                modifier =
                    Modifier.width(100.dp)
                        .height(100.dp)
                        .background(color = if (index % 2 == 0) Color.Red else Color.Green)
            ) {
                Text(text = data[index])
            }
        }
    }
    val items = (0..100).toList().map { it.toString() }
    val itemProvider = remember(items) { CustomItemProvider(items) }
}