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.

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.layout.LazyLayout
import androidx.compose.foundation.lazy.layout.LazyLayoutItemProvider
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

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

Summary

Public functions

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

The item for the given index and key.

Cmn
open Any?
getContentType(index: @IntRange(from = 0) Int)

Returns the content type for the item on this index.

Cmn
open Int
getIndex(key: Any)

Get index for given key.

Cmn
open Any
getKey(index: @IntRange(from = 0) Int)

Returns the key for the item on this index.

Cmn

Public properties

Int

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

Cmn

Public functions

Item

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

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
index: @IntRange(from = 0) Int

the index of the item in the list

key: Any

The key of the item as described above.

getContentType

open fun getContentType(index: @IntRange(from = 0) 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
index: @IntRange(from = 0) Int

the index of an item in the layout.

Returns
Any?

The content type mapped from index.

getIndex

open 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
key: Any

the key of an item in the layout.

Returns
Int

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

getKey

open fun getKey(index: @IntRange(from = 0) Int): Any

Returns the key for the item on this index.

Parameters
index: @IntRange(from = 0) Int

the index of an item in the layout.

Returns
Any

The key mapped from index.

See also
getDefaultLazyLayoutKey

which you can use if the user didn't provide a key.

Public properties

itemCount

val itemCountInt

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