# androidx.compose.foundation.lazy.grid

Common/All

## Interfaces

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/GridCells` | This class describes the count and the sizes of columns in vertical grids, or rows in horizontal grids. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemInfo` | Contains useful information about an individual item in lazy grids like `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyVerticalGrid.composable#LazyVerticalGrid(androidx.compose.foundation.lazy.grid.GridCells,androidx.compose.ui.Modifier,androidx.compose.foundation.lazy.grid.LazyGridState,androidx.compose.foundation.layout.PaddingValues,kotlin.Boolean,androidx.compose.foundation.layout.Arrangement.Vertical,androidx.compose.foundation.layout.Arrangement.Horizontal,androidx.compose.foundation.gestures.FlingBehavior,kotlin.Boolean,androidx.compose.foundation.OverscrollEffect,androidx.compose.foundation.lazy.layout.LazyLayoutCacheWindow,kotlin.Function1)`. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemScope` | Receiver scope being used by the item content parameter of `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyVerticalGrid.composable#LazyVerticalGrid(androidx.compose.foundation.lazy.grid.GridCells,androidx.compose.ui.Modifier,androidx.compose.foundation.lazy.grid.LazyGridState,androidx.compose.foundation.layout.PaddingValues,kotlin.Boolean,androidx.compose.foundation.layout.Arrangement.Vertical,androidx.compose.foundation.layout.Arrangement.Horizontal,androidx.compose.foundation.gestures.FlingBehavior,kotlin.Boolean,androidx.compose.foundation.OverscrollEffect,androidx.compose.foundation.lazy.layout.LazyLayoutCacheWindow,kotlin.Function1)`. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemSpanScope` | Scope of lambdas used to calculate the spans of items in lazy grids. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridLayoutInfo` | Contains useful information about the currently displayed layout state of lazy grids like `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyVerticalGrid.composable#LazyVerticalGrid(androidx.compose.foundation.lazy.grid.GridCells,androidx.compose.ui.Modifier,androidx.compose.foundation.lazy.grid.LazyGridState,androidx.compose.foundation.layout.PaddingValues,kotlin.Boolean,androidx.compose.foundation.layout.Arrangement.Vertical,androidx.compose.foundation.layout.Arrangement.Horizontal,androidx.compose.foundation.gestures.FlingBehavior,kotlin.Boolean,androidx.compose.foundation.OverscrollEffect,androidx.compose.foundation.lazy.layout.LazyLayoutCacheWindow,kotlin.Function1)`. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridPrefetchResultScope` | **This interface is deprecated.** LazyGridPrefetchResultScope is deprecated alongside LazyGridPrefetchStrategy. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridPrefetchScope` | **This interface is deprecated.** LazyGridPrefetchScope is deprecated alongside LazyGridPrefetchStrategy. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridPrefetchStrategy` | **This interface is deprecated.** LazyGridPrefetchStrategy is deprecated in favor of LazyLayoutCacheWindow. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridScope` | Receiver scope which is used by `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyVerticalGrid.composable#LazyVerticalGrid(androidx.compose.foundation.lazy.grid.GridCells,androidx.compose.ui.Modifier,androidx.compose.foundation.lazy.grid.LazyGridState,androidx.compose.foundation.layout.PaddingValues,kotlin.Boolean,androidx.compose.foundation.layout.Arrangement.Vertical,androidx.compose.foundation.layout.Arrangement.Horizontal,androidx.compose.foundation.gestures.FlingBehavior,kotlin.Boolean,androidx.compose.foundation.OverscrollEffect,androidx.compose.foundation.lazy.layout.LazyLayoutCacheWindow,kotlin.Function1)`. | Cmn |

## Classes

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/GridCells.Adaptive` | Defines a grid with as many rows or columns as possible on the condition that every cell has at least `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/GridCells.Adaptive#minSize()` space and all extra space distributed evenly. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/GridCells.Fixed` | Defines a grid with fixed number of rows or columns. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/GridCells.FixedSize` | Defines a grid with as many rows or columns as possible on the condition that every cell takes exactly `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/GridCells.FixedSize#size()` space. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/GridItemSpan` | Represents the span of an item in a `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyVerticalGrid.composable#LazyVerticalGrid(androidx.compose.foundation.lazy.grid.GridCells,androidx.compose.ui.Modifier,androidx.compose.foundation.lazy.grid.LazyGridState,androidx.compose.foundation.layout.PaddingValues,kotlin.Boolean,androidx.compose.foundation.layout.Arrangement.Vertical,androidx.compose.foundation.layout.Arrangement.Horizontal,androidx.compose.foundation.gestures.FlingBehavior,kotlin.Boolean,androidx.compose.foundation.OverscrollEffect,androidx.compose.foundation.lazy.layout.LazyLayoutCacheWindow,kotlin.Function1)` or a `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyHorizontalGrid.composable#LazyHorizontalGrid(androidx.compose.foundation.lazy.grid.GridCells,androidx.compose.ui.Modifier,androidx.compose.foundation.lazy.grid.LazyGridState,androidx.compose.foundation.layout.PaddingValues,kotlin.Boolean,androidx.compose.foundation.layout.Arrangement.Horizontal,androidx.compose.foundation.layout.Arrangement.Vertical,androidx.compose.foundation.gestures.FlingBehavior,kotlin.Boolean,androidx.compose.foundation.OverscrollEffect,androidx.compose.foundation.lazy.layout.LazyLayoutCacheWindow,kotlin.Function1)`. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridState` |   | Cmn |

## Objects

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridDefaults` | Contains the default values used by `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyVerticalGrid.composable#LazyVerticalGrid(androidx.compose.foundation.lazy.grid.GridCells,androidx.compose.ui.Modifier,androidx.compose.foundation.lazy.grid.LazyGridState,androidx.compose.foundation.layout.PaddingValues,kotlin.Boolean,androidx.compose.foundation.layout.Arrangement.Vertical,androidx.compose.foundation.layout.Arrangement.Horizontal,androidx.compose.foundation.gestures.FlingBehavior,kotlin.Boolean,androidx.compose.foundation.OverscrollEffect,androidx.compose.foundation.lazy.layout.LazyLayoutCacheWindow,kotlin.Function1)` and `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyHorizontalGrid.composable#LazyHorizontalGrid(androidx.compose.foundation.lazy.grid.GridCells,androidx.compose.ui.Modifier,androidx.compose.foundation.lazy.grid.LazyGridState,androidx.compose.foundation.layout.PaddingValues,kotlin.Boolean,androidx.compose.foundation.layout.Arrangement.Horizontal,androidx.compose.foundation.layout.Arrangement.Vertical,androidx.compose.foundation.gestures.FlingBehavior,kotlin.Boolean,androidx.compose.foundation.OverscrollEffect,androidx.compose.foundation.lazy.layout.LazyLayoutCacheWindow,kotlin.Function1)`. | Cmn |

## Annotations

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridScopeMarker` | DSL marker used to distinguish between lazy grid dsl scope and the item content scope. | Cmn |

## Composables

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyHorizontalGrid.composable` | A lazy horizontal grid layout. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyVerticalGrid.composable` | A lazy vertical grid layout. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/rememberLazyGridState.composable` | Creates a `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridState` that is remembered across compositions. | Cmn |

## Top-level functions summary

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/GridItemSpan` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/package-summary#GridItemSpan(kotlin.Int)(currentLineSpan: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 1) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Creates a `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/package-summary#GridItemSpan(kotlin.Int)` with a specified `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/package-summary#GridItemSpan(kotlin.Int)`. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridPrefetchStrategy` | `@https://developer.android.com/reference/kotlin/androidx/compose/foundation/ExperimentalFoundationApi [LazyGridPrefetchStrategy](https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/package-summary#LazyGridPrefetchStrategy(kotlin.Int))(nestedPrefetchItemCount: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` **This function is deprecated.** LazyGridPrefetchStrategy is deprecated. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/LazyLayoutScrollScope` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/package-summary#LazyLayoutScrollScope(androidx.compose.foundation.lazy.grid.LazyGridState,androidx.compose.foundation.gestures.ScrollScope)(state: https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridState, scrollScope: https://developer.android.com/reference/kotlin/androidx/compose/foundation/gestures/ScrollScope)` An implementation of `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/package-summary#LazyLayoutScrollScope(androidx.compose.foundation.lazy.grid.LazyGridState,androidx.compose.foundation.gestures.ScrollScope)` that can be used with LazyGrids. | Cmn |

## Extension functions summary

|---|---|---|
| `inline https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridScope.https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/package-summary#(androidx.compose.foundation.lazy.grid.LazyGridScope).items(kotlin.Array,kotlin.Function1,kotlin.Function2,kotlin.Function1,kotlin.Function2)( items: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<T>, noinline key: ((item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)?, noinline span: (https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemSpanScope.(item) -> https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/GridItemSpan)?, noinline contentType: (item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, crossinline itemContent: @https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemScope.(item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html )` Adds an array of items. | Cmn |
| `inline https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridScope.https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/package-summary#(androidx.compose.foundation.lazy.grid.LazyGridScope).items(kotlin.collections.List,kotlin.Function1,kotlin.Function2,kotlin.Function1,kotlin.Function2)( items: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<T>, noinline key: ((item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)?, noinline span: (https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemSpanScope.(item) -> https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/GridItemSpan)?, noinline contentType: (item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, crossinline itemContent: @https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemScope.(item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html )` Adds a list of items. | Cmn |
| `inline https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridScope.https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/package-summary#(androidx.compose.foundation.lazy.grid.LazyGridScope).itemsIndexed(kotlin.Array,kotlin.Function2,kotlin.Function3,kotlin.Function2,kotlin.Function3)( items: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<T>, noinline key: ((index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)?, noinline span: (https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemSpanScope.(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, item) -> https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/GridItemSpan)?, crossinline contentType: (index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, crossinline itemContent: @https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemScope.(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html )` Adds an array of items where the content of an item is aware of its index. | Cmn |
| `inline https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridScope.https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/package-summary#(androidx.compose.foundation.lazy.grid.LazyGridScope).itemsIndexed(kotlin.collections.List,kotlin.Function2,kotlin.Function3,kotlin.Function2,kotlin.Function3)( items: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<T>, noinline key: ((index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)?, noinline span: (https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemSpanScope.(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, item) -> https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/GridItemSpan)?, crossinline contentType: (index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, crossinline itemContent: @https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemScope.(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html )` Adds a list of items where the content of an item is aware of its index. | Cmn |

## Top-level functions

### GridItemSpan

Cmn 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/grid/LazyGridSpan.kt+function:GridItemSpan) Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.2.0)

```
fun GridItemSpan(currentLineSpan: @IntRange(from = 1) Int): GridItemSpan
```

Creates a `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/package-summary#GridItemSpan(kotlin.Int)` with a specified `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/package-summary#GridItemSpan(kotlin.Int)`. This will be the horizontal span for an item of a `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyVerticalGrid.composable#LazyVerticalGrid(androidx.compose.foundation.lazy.grid.GridCells,androidx.compose.ui.Modifier,androidx.compose.foundation.lazy.grid.LazyGridState,androidx.compose.foundation.layout.PaddingValues,kotlin.Boolean,androidx.compose.foundation.layout.Arrangement.Vertical,androidx.compose.foundation.layout.Arrangement.Horizontal,androidx.compose.foundation.gestures.FlingBehavior,kotlin.Boolean,androidx.compose.foundation.OverscrollEffect,androidx.compose.foundation.lazy.layout.LazyLayoutCacheWindow,kotlin.Function1)` and the vertical span for a `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyHorizontalGrid.composable#LazyHorizontalGrid(androidx.compose.foundation.lazy.grid.GridCells,androidx.compose.ui.Modifier,androidx.compose.foundation.lazy.grid.LazyGridState,androidx.compose.foundation.layout.PaddingValues,kotlin.Boolean,androidx.compose.foundation.layout.Arrangement.Horizontal,androidx.compose.foundation.layout.Arrangement.Vertical,androidx.compose.foundation.gestures.FlingBehavior,kotlin.Boolean,androidx.compose.foundation.OverscrollEffect,androidx.compose.foundation.lazy.layout.LazyLayoutCacheWindow,kotlin.Function1)`.

### LazyGridPrefetchStrategy

Cmn 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/grid/LazyGridPrefetchStrategy.kt+function:LazyGridPrefetchStrategy) Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.7.0) Deprecated in [1.13.0-rc01](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.13.0-rc01)

```
@ExperimentalFoundationApi
fun [LazyGridPrefetchStrategy](https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/package-summary#LazyGridPrefetchStrategy(kotlin.Int))(nestedPrefetchItemCount: Int = 2): LazyGridPrefetchStrategy
```

> [!CAUTION]
> **This function is deprecated.**   
> LazyGridPrefetchStrategy is deprecated. Prefetching behavior should be configured using LazyLayoutCacheWindow on LazyVerticalGrid or LazyHorizontalGrid.

Creates an instance of the default `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/package-summary#LazyGridPrefetchStrategy(kotlin.Int)`, allowing for customization of the nested prefetch count.

| Parameters |
|---|---|
| `nestedPrefetchItemCount: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = 2` | specifies how many inner items should be prefetched when this LazyGrid is nested inside another LazyLayout. For example, if this is the state for a horizontal LazyGrid nested in a vertical LazyGrid, you might want to set this to the number of items that will be visible when this grid is scrolled into view. If automatic nested prefetch is enabled, this value will be used as the initial count and the strategy will adapt the count automatically. |

### LazyLayoutScrollScope

Cmn 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/grid/LazyGridScrollScope.kt+function:LazyLayoutScrollScope) Added in [1.8.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.8.0)

```
fun LazyLayoutScrollScope(state: LazyGridState, scrollScope: ScrollScope): LazyLayoutScrollScope
```

An implementation of `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/package-summary#LazyLayoutScrollScope(androidx.compose.foundation.lazy.grid.LazyGridState,androidx.compose.foundation.gestures.ScrollScope)` that can be used with LazyGrids.

```kotlin
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyGridState
import androidx.compose.foundation.lazy.grid.LazyHorizontalGrid
import androidx.compose.foundation.lazy.grid.LazyLayoutScrollScope
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.foundation.lazy.layout.LazyLayoutScrollScope
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

suspend fun LazyGridState.customScroll(block: suspend LazyLayoutScrollScope.() -> Unit) =
    scroll {
        block.invoke(LazyLayoutScrollScope(this@customScroll, this))
    }

val itemsList = (0..100).toList()
val state = rememberLazyGridState()
val scope = rememberCoroutineScope()

Column(Modifier.verticalScroll(rememberScrollState())) {
    Button(
        onClick = {
            scope.launch {
                state.customScroll {
                    snapToItem(40, 0) // teleport to item 40
                    val distance = calculateDistanceTo(50).toFloat()
                    var previousValue = 0f
                    androidx.compose.animation.core.animate(
                        0f,
                        distance,
                        animationSpec = tween(5_000),
                    ) { currentValue, _ ->
                        previousValue += scrollBy(currentValue - previousValue)
                    }
                }
            }
        }
    ) {
        Text("Scroll To Item 50")
    }
    LazyHorizontalGrid(
        state = state,
        rows = GridCells.Fixed(3),
        modifier = Modifier.height(600.dp).fillMaxWidth(),
    ) {
        items(itemsList) {
            Box(Modifier.padding(2.dp).background(Color.Red).size(45.dp)) {
                Text(it.toString())
            }
        }
    }
}
```

| Parameters |
|---|---|
| `state: https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridState` | The `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridState` associated with the layout where this custom scroll should be performed. |
| `scrollScope: https://developer.android.com/reference/kotlin/androidx/compose/foundation/gestures/ScrollScope` | The base `https://developer.android.com/reference/kotlin/androidx/compose/foundation/gestures/ScrollScope` where the scroll session was created. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/LazyLayoutScrollScope` | An implementation of `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/package-summary#LazyLayoutScrollScope(androidx.compose.foundation.lazy.grid.LazyGridState,androidx.compose.foundation.gestures.ScrollScope)` that works with `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyHorizontalGrid.composable#LazyHorizontalGrid(androidx.compose.foundation.lazy.grid.GridCells,androidx.compose.ui.Modifier,androidx.compose.foundation.lazy.grid.LazyGridState,androidx.compose.foundation.layout.PaddingValues,kotlin.Boolean,androidx.compose.foundation.layout.Arrangement.Horizontal,androidx.compose.foundation.layout.Arrangement.Vertical,androidx.compose.foundation.gestures.FlingBehavior,kotlin.Boolean,androidx.compose.foundation.OverscrollEffect,androidx.compose.foundation.lazy.layout.LazyLayoutCacheWindow,kotlin.Function1)` and `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyVerticalGrid.composable#LazyVerticalGrid(androidx.compose.foundation.lazy.grid.GridCells,androidx.compose.ui.Modifier,androidx.compose.foundation.lazy.grid.LazyGridState,androidx.compose.foundation.layout.PaddingValues,kotlin.Boolean,androidx.compose.foundation.layout.Arrangement.Vertical,androidx.compose.foundation.layout.Arrangement.Horizontal,androidx.compose.foundation.gestures.FlingBehavior,kotlin.Boolean,androidx.compose.foundation.OverscrollEffect,androidx.compose.foundation.lazy.layout.LazyLayoutCacheWindow,kotlin.Function1)`. |

## Extension functions

### LazyGridScope.items

Cmn 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/grid/LazyGridDsl.kt+function:items) Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.2.0)

```
inline fun <T : Any?> LazyGridScope.items(
    items: Array<T>,
    noinline key: ((item) -> Any)? = null,
    noinline span: (LazyGridItemSpanScope.(item) -> GridItemSpan)? = null,
    noinline contentType: (item) -> Any? = { null },
    crossinline itemContent: @Composable LazyGridItemScope.(item) -> Unit
): Unit
```

Adds an array of items.

| Parameters |
|---|---|
| `items: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<T>` | the data array |
| `noinline key: ((item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)? = null` | a factory of stable and unique keys representing the item. Using the same key for multiple items in the grid is not allowed. Type of the key should be saveable via Bundle on Android. If null is passed the position in the grid will represent the key. 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.This can be overridden by calling `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridState#requestScrollToItem(kotlin.Int,kotlin.Int)`. |
| `noinline span: (https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemSpanScope.(item) -> https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/GridItemSpan)? = null` | define custom spans for the items. Default is 1x1. It is good practice to leave it `null` when this matches the intended behavior, as providing a custom implementation impacts performance |
| `noinline contentType: (item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html? = { 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. |
| `crossinline itemContent: @https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemScope.(item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | the content displayed by a single item |

### LazyGridScope.items

Cmn 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/grid/LazyGridDsl.kt+function:items)

```
inline fun <T : Any?> LazyGridScope.items(
    items: List<T>,
    noinline key: ((item) -> Any)? = null,
    noinline span: (LazyGridItemSpanScope.(item) -> GridItemSpan)? = null,
    noinline contentType: (item) -> Any? = { null },
    crossinline itemContent: @Composable LazyGridItemScope.(item) -> Unit
): Unit
```

Adds a list of items.

| Parameters |
|---|---|
| `items: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<T>` | the data list |
| `noinline key: ((item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)? = null` | a factory of stable and unique keys representing the item. Using the same key for multiple items in the grid is not allowed. Type of the key should be saveable via Bundle on Android. If null is passed the position in the grid will represent the key. 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. This can be overridden by calling `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridState#requestScrollToItem(kotlin.Int,kotlin.Int)`. |
| `noinline span: (https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemSpanScope.(item) -> https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/GridItemSpan)? = null` | define custom spans for the items. Default is 1x1. It is good practice to leave it `null` when this matches the intended behavior, as providing a custom implementation impacts performance |
| `noinline contentType: (item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html? = { 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. |
| `crossinline itemContent: @https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemScope.(item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | the content displayed by a single item |

### LazyGridScope.itemsIndexed

Cmn 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/grid/LazyGridDsl.kt+function:itemsIndexed) Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.2.0)

```
inline fun <T : Any?> LazyGridScope.itemsIndexed(
    items: Array<T>,
    noinline key: ((index: Int, item) -> Any)? = null,
    noinline span: (LazyGridItemSpanScope.(index: Int, item) -> GridItemSpan)? = null,
    crossinline contentType: (index: Int, item) -> Any? = { _, _ -> null },
    crossinline itemContent: @Composable LazyGridItemScope.(index: Int, item) -> Unit
): Unit
```

Adds an array of items where the content of an item is aware of its index.

| Parameters |
|---|---|
| `items: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<T>` | the data array |
| `noinline key: ((index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)? = null` | a factory of stable and unique keys representing the item. Using the same key for multiple items in the grid is not allowed. Type of the key should be saveable via Bundle on Android. If null is passed the position in the grid will represent the key. 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. This can be overridden by calling `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridState#requestScrollToItem(kotlin.Int,kotlin.Int)`. |
| `noinline span: (https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemSpanScope.(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, item) -> https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/GridItemSpan)? = null` | define custom spans for the items. Default is 1x1. It is good practice to leave it `null` when this matches the intended behavior, as providing a custom implementation impacts performance |
| `crossinline contentType: (index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html? = { _, _ -> 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. |
| `crossinline itemContent: @https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemScope.(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | the content displayed by a single item |

### LazyGridScope.itemsIndexed

Cmn 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/grid/LazyGridDsl.kt+function:itemsIndexed)

```
inline fun <T : Any?> LazyGridScope.itemsIndexed(
    items: List<T>,
    noinline key: ((index: Int, item) -> Any)? = null,
    noinline span: (LazyGridItemSpanScope.(index: Int, item) -> GridItemSpan)? = null,
    crossinline contentType: (index: Int, item) -> Any? = { _, _ -> null },
    crossinline itemContent: @Composable LazyGridItemScope.(index: Int, item) -> Unit
): Unit
```

Adds a list of items where the content of an item is aware of its index.

| Parameters |
|---|---|
| `items: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<T>` | the data list |
| `noinline key: ((index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)? = null` | a factory of stable and unique keys representing the item. Using the same key for multiple items in the grid is not allowed. Type of the key should be saveable via Bundle on Android. If null is passed the position in the grid will represent the key. 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. This can be overridden by calling `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridState#requestScrollToItem(kotlin.Int,kotlin.Int)`. |
| `noinline span: (https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemSpanScope.(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, item) -> https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/GridItemSpan)? = null` | define custom spans for the items. Default is 1x1. It is good practice to leave it `null` when this matches the intended behavior, as providing a custom implementation impacts performance |
| `crossinline contentType: (index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html? = { _, _ -> 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. |
| `crossinline itemContent: @https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemScope.(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, item) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | the content displayed by a single item |