# LazyGridPrefetchStrategy

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+class:androidx.compose.foundation.lazy.grid.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) Common/All
- Cmn

  ```
  @ExperimentalFoundationApi
  interface LazyGridPrefetchStrategy
  ```

*** ** * ** ***

> [!CAUTION]
> **This interface is deprecated.**   
> LazyGridPrefetchStrategy is deprecated in favor of LazyLayoutCacheWindow. LazyLayoutCacheWindow provides a declarative, viewport-based prefetching window passed directly to LazyVerticalGrid or LazyHorizontalGrid.

Implementations of this interface control which indices of a LazyGrid should be prefetched (precomposed and premeasured during idle time) as the user interacts with it.

Implementations should invoke `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridPrefetchScope#scheduleLinePrefetch(kotlin.Int)` to schedule prefetches from the `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridPrefetchStrategy#(androidx.compose.foundation.lazy.grid.LazyGridPrefetchScope).onScroll(kotlin.Float,androidx.compose.foundation.lazy.grid.LazyGridLayoutInfo)` and `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridPrefetchStrategy#(androidx.compose.foundation.lazy.grid.LazyGridPrefetchScope).onVisibleItemsUpdated(androidx.compose.foundation.lazy.grid.LazyGridLayoutInfo)` callbacks. If any of the returned PrefetchHandles no longer need to be prefetched, use `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/LazyLayoutPrefetchState.PrefetchHandle#cancel()` to cancel the request.

```kotlin
import androidx.compose.foundation.layout.height
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.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.foundation.lazy.layout.LazyLayoutCacheWindow
import androidx.compose.material3.Text
import androidx.compose.runtime.remember
import androidx.compose.ui.unit.Density

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

// Migrating from LazyGridPrefetchStrategy (which scheduled prefetching for individual
// items/lines):
// Use LazyLayoutCacheWindow to dynamically calculate the prefetch cache window in pixels based
// on average item size from LazyGridState layoutInfo.
val customCacheWindow =
    remember(state) {
        object : LazyLayoutCacheWindow {
            override fun Density.calculateAheadWindow(viewport: Int): Int {
                val visibleItems = state.layoutInfo.visibleItemsInfo
                if (visibleItems.isEmpty()) return 0
                val averageItemHeight =
                    visibleItems.sumOf { it.size.height } / visibleItems.size
                // Prefetch 1 line ahead based on average item height
                return averageItemHeight
            }
        }
    }

LazyVerticalGrid(columns = GridCells.Fixed(3), state = state, cacheWindow = customCacheWindow) {
    items(itemsList) { item -> Text("Item $item") }
}
```

## Summary

| ### Public functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/NestedPrefetchScope.[onNestedPrefetch](https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridPrefetchStrategy#(androidx.compose.foundation.lazy.layout.NestedPrefetchScope).onNestedPrefetch(kotlin.Int))(firstVisibleItemIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` **This function is deprecated.** LazyGridPrefetchStrategy is deprecated in favor of LazyLayoutCacheWindow. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridPrefetchScope.[onScroll](https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridPrefetchStrategy#(androidx.compose.foundation.lazy.grid.LazyGridPrefetchScope).onScroll(kotlin.Float,androidx.compose.foundation.lazy.grid.LazyGridLayoutInfo))( delta: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html, layoutInfo: https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridLayoutInfo )` **This function is deprecated.** LazyGridPrefetchStrategy is deprecated in favor of LazyLayoutCacheWindow. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridPrefetchScope.[onVisibleItemsUpdated](https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridPrefetchStrategy#(androidx.compose.foundation.lazy.grid.LazyGridPrefetchScope).onVisibleItemsUpdated(androidx.compose.foundation.lazy.grid.LazyGridLayoutInfo))( layoutInfo: https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridLayoutInfo )` **This function is deprecated.** LazyGridPrefetchStrategy is deprecated in favor of LazyLayoutCacheWindow. | Cmn |

| ### Public properties |
|---|---|---|
| `open https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/PrefetchScheduler?` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridPrefetchStrategy#prefetchScheduler()` **This property is deprecated.** Customization of PrefetchScheduler is no longer supported. | Cmn |

## Public functions

### NestedPrefetchScope.onNestedPrefetch

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:onNestedPrefetch) 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)

```
fun NestedPrefetchScope.[onNestedPrefetch](https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridPrefetchStrategy#(androidx.compose.foundation.lazy.layout.NestedPrefetchScope).onNestedPrefetch(kotlin.Int))(firstVisibleItemIndex: Int): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
> LazyGridPrefetchStrategy is deprecated in favor of LazyLayoutCacheWindow. LazyLayoutCacheWindow provides a declarative, viewport-based prefetching window passed directly to LazyVerticalGrid or LazyHorizontalGrid.

onNestedPrefetch is invoked when a parent LazyLayout has prefetched content which contains this LazyGrid. It gives this LazyGrid a chance to request prefetch for some of its own children before coming onto screen.

Implementations can use `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/NestedPrefetchScope#schedulePrefetch(kotlin.Int)` to schedule child prefetches. For example, this is useful if this LazyGrid is a LazyRow that is a child of a LazyColumn: in that case, `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridPrefetchStrategy#(androidx.compose.foundation.lazy.layout.NestedPrefetchScope).onNestedPrefetch(kotlin.Int)` can schedule the children it expects to be visible when it comes onto screen, giving the LazyLayout infra a chance to compose these children ahead of time and reduce jank.

Generally speaking, `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridPrefetchStrategy#(androidx.compose.foundation.lazy.layout.NestedPrefetchScope).onNestedPrefetch(kotlin.Int)` should only request prefetch for children that it expects to actually be visible when this grid is scrolled into view.

| Parameters |
|---|---|
| `firstVisibleItemIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | the index of the first visible item. It should be used to start prefetching from the correct index in case the grid has been created at a non-zero offset. |

### LazyGridPrefetchScope.onScroll

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:onScroll) 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)

```
fun LazyGridPrefetchScope.[onScroll](https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridPrefetchStrategy#(androidx.compose.foundation.lazy.grid.LazyGridPrefetchScope).onScroll(kotlin.Float,androidx.compose.foundation.lazy.grid.LazyGridLayoutInfo))(
    delta: Float,
    layoutInfo: LazyGridLayoutInfo
): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
> LazyGridPrefetchStrategy is deprecated in favor of LazyLayoutCacheWindow. LazyLayoutCacheWindow provides a declarative, viewport-based prefetching window passed directly to LazyVerticalGrid or LazyHorizontalGrid.

onScroll is invoked when the LazyGrid scrolls, whether or not the visible items have changed. If the visible items have also changed, then this will be invoked in the same frame *after* `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridPrefetchStrategy#(androidx.compose.foundation.lazy.grid.LazyGridPrefetchScope).onVisibleItemsUpdated(androidx.compose.foundation.lazy.grid.LazyGridLayoutInfo)`.

| Parameters |
|---|---|
| `delta: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | the change in scroll direction. Delta \< 0 indicates scrolling down while delta \> 0 indicates scrolling up. |
| `layoutInfo: https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridLayoutInfo` | the current `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridLayoutInfo` |

### LazyGridPrefetchScope.onVisibleItemsUpdated

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:onVisibleItemsUpdated) 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)

```
fun LazyGridPrefetchScope.[onVisibleItemsUpdated](https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridPrefetchStrategy#(androidx.compose.foundation.lazy.grid.LazyGridPrefetchScope).onVisibleItemsUpdated(androidx.compose.foundation.lazy.grid.LazyGridLayoutInfo))(
    layoutInfo: LazyGridLayoutInfo
): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
> LazyGridPrefetchStrategy is deprecated in favor of LazyLayoutCacheWindow. LazyLayoutCacheWindow provides a declarative, viewport-based prefetching window passed directly to LazyVerticalGrid or LazyHorizontalGrid.

onVisibleItemsUpdated is invoked when the LazyGrid scrolls if the visible items have changed.

| Parameters |
|---|---|
| `layoutInfo: https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridLayoutInfo` | the current `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridLayoutInfo`. Info about the updated visible items can be found in `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/grid/LazyGridLayoutInfo#visibleItemsInfo()`. |

## Public properties

### prefetchScheduler

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

```
open val prefetchScheduler: PrefetchScheduler?
```

> [!CAUTION]
> **This property is deprecated.**   
> Customization of PrefetchScheduler is no longer supported. LazyLayout will attach an appropriate scheduler internally.

A `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/PrefetchScheduler` implementation which will be used to execute prefetch requests for this strategy implementation. If null, the default `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/layout/PrefetchScheduler` for the platform will be used.