# LazyLayoutItemProvider

Artifact: [androidx.compose.foundation:foundation](https://developer.android.com/jetpack/androidx/releases/compose-foundation) [View Source](https://cs.android.com/search?q=file:androidx/compose/foundation/lazy/layout/LazyLayoutItemProvider.kt+class:androidx.compose.foundation.lazy.layout.LazyLayoutItemProvider) Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.2.0) Common/AllAndroid/JVM
- Cmn

  ```
  interface LazyLayoutItemProvider
  ```

*** ** * ** ***

Provides all the needed info about the items which could be later composed and displayed as children or `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/LazyLayout.composable#LazyLayout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.foundation.lazy.layout.LazyLayoutPrefetchState,androidx.compose.foundation.lazy.layout.LazyLayoutMeasurePolicy)`. The number of virtual items is limited by `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/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 `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/LazyLayout.composable#LazyLayout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.foundation.lazy.layout.LazyLayoutPrefetchState,androidx.compose.foundation.lazy.layout.LazyLayoutMeasurePolicy)` for additional context.

```kotlin
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.material3.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 |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/LazyLayoutItemProvider#Item(kotlin.Int,kotlin.Any)(index: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)` The item for the given `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/LazyLayoutItemProvider#Item(kotlin.Int,kotlin.Any)` and `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/LazyLayoutItemProvider#Item(kotlin.Int,kotlin.Any)`. | Cmn |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/LazyLayoutItemProvider#getContentType(kotlin.Int)(index: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns the content type for the item on this index. | Cmn |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/LazyLayoutItemProvider#getIndex(kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)` Get index for given key. | Cmn |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/LazyLayoutItemProvider#getKey(kotlin.Int)(index: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns the key for the item on this index. | Cmn |

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/LazyLayoutItemProvider#itemCount()` The total number of items in the lazy layout (visible or not). | Cmn |

## Public functions

### Item

Cmn Added in [1.5.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.5.0)

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

The item for the given `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/LazyLayoutItemProvider#Item(kotlin.Int,kotlin.Any)` and `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/LazyLayoutItemProvider#Item(kotlin.Int,kotlin.Any)`. 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 `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/LazyLayoutItemProvider#getKey(kotlin.Int)` function. If a key concept is not needed by your LazyLayout implementation (`https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/LazyLayoutItemProvider#getKey(kotlin.Int)` is not overridden), the internal implementation will simply use `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/package-summary#getDefaultLazyLayoutKey(kotlin.Int)` 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: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | the index of the item in the list |
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html` | The key of the item as described above. |

### getContentType

Cmn Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.2.0)

```
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: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | the index of an item in the layout. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?` | The content type mapped from `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/LazyLayoutItemProvider#getContentType(kotlin.Int)`. |

### getIndex

Cmn Added in [1.5.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.5.0)

```
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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html` | the key of an item in the layout. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The index mapped from `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/LazyLayoutItemProvider#getIndex(kotlin.Any)` if it is present in the layout, otherwise -1. |

### getKey

Cmn Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.2.0)

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

Returns the key for the item on this index.

| Parameters |
|---|---|
| `index: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | the index of an item in the layout. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html` | The key mapped from `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/LazyLayoutItemProvider#getKey(kotlin.Int)`. |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/package-summary#getDefaultLazyLayoutKey(kotlin.Int)` | which you can use if the user didn't provide a key. |

## Public properties

### itemCount

Cmn Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.2.0)

```
val itemCount: Int
```

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