# LazyListPrefetchStrategy

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/LazyListPrefetchStrategy.kt+class:androidx.compose.foundation.lazy.LazyListPrefetchStrategy) 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 LazyListPrefetchStrategy
  ```

*** ** * ** ***

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

Implementations of this interface control which indices of a LazyList 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/LazyListPrefetchScope#schedulePrefetch(kotlin.Int,kotlin.Function1)` to schedule prefetches from the `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/LazyListPrefetchStrategy#(androidx.compose.foundation.lazy.LazyListPrefetchScope).onScroll(kotlin.Float,androidx.compose.foundation.lazy.LazyListLayoutInfo)` and `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/LazyListPrefetchStrategy#(androidx.compose.foundation.lazy.LazyListPrefetchScope).onVisibleItemsUpdated(androidx.compose.foundation.lazy.LazyListLayoutInfo)` 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.Column
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.layout.LazyLayoutCacheWindow
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.Text
import androidx.compose.runtime.State
import androidx.compose.runtime.remember
import androidx.compose.ui.unit.Density

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

// Migrating from LazyListPrefetchStrategy (which scheduled prefetching for individual items):
// Use LazyLayoutCacheWindow to dynamically calculate the prefetch cache window in pixels based
// on average item size from LazyListState 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 } / visibleItems.size
                // Prefetch 1 item ahead based on average item size
                return averageItemHeight
            }
        }
    }

LazyColumn(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/LazyListPrefetchStrategy#(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.** LazyListPrefetchStrategy 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/LazyListPrefetchScope.[onScroll](https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/LazyListPrefetchStrategy#(androidx.compose.foundation.lazy.LazyListPrefetchScope).onScroll(kotlin.Float,androidx.compose.foundation.lazy.LazyListLayoutInfo))( delta: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html, layoutInfo: https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/LazyListLayoutInfo )` **This function is deprecated.** LazyListPrefetchStrategy 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/LazyListPrefetchScope.[onVisibleItemsUpdated](https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/LazyListPrefetchStrategy#(androidx.compose.foundation.lazy.LazyListPrefetchScope).onVisibleItemsUpdated(androidx.compose.foundation.lazy.LazyListLayoutInfo))( layoutInfo: https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/LazyListLayoutInfo )` **This function is deprecated.** LazyListPrefetchStrategy 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/LazyListPrefetchStrategy#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/LazyListPrefetchStrategy.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/LazyListPrefetchStrategy#(androidx.compose.foundation.lazy.layout.NestedPrefetchScope).onNestedPrefetch(kotlin.Int))(firstVisibleItemIndex: Int): Unit
```

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

onNestedPrefetch is invoked when a parent LazyLayout has prefetched content which contains this LazyList. It gives this LazyList 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 LazyList is a LazyRow that is a child of a LazyColumn: in that case, `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/LazyListPrefetchStrategy#(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/LazyListPrefetchStrategy#(androidx.compose.foundation.lazy.layout.NestedPrefetchScope).onNestedPrefetch(kotlin.Int)` should only request prefetch for children that it expects to actually be visible when this list 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 list has been created at a non-zero offset. |

### LazyListPrefetchScope.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/LazyListPrefetchStrategy.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 LazyListPrefetchScope.[onScroll](https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/LazyListPrefetchStrategy#(androidx.compose.foundation.lazy.LazyListPrefetchScope).onScroll(kotlin.Float,androidx.compose.foundation.lazy.LazyListLayoutInfo))(
    delta: Float,
    layoutInfo: LazyListLayoutInfo
): Unit
```

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

onScroll is invoked when the LazyList 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/LazyListPrefetchStrategy#(androidx.compose.foundation.lazy.LazyListPrefetchScope).onVisibleItemsUpdated(androidx.compose.foundation.lazy.LazyListLayoutInfo)`.

| 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/LazyListLayoutInfo` | the current `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/LazyListLayoutInfo` |

### LazyListPrefetchScope.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/LazyListPrefetchStrategy.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 LazyListPrefetchScope.[onVisibleItemsUpdated](https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/LazyListPrefetchStrategy#(androidx.compose.foundation.lazy.LazyListPrefetchScope).onVisibleItemsUpdated(androidx.compose.foundation.lazy.LazyListLayoutInfo))(
    layoutInfo: LazyListLayoutInfo
): Unit
```

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

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

| Parameters |
|---|---|
| `layoutInfo: https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/LazyListLayoutInfo` | the current `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/LazyListLayoutInfo`. Info about the updated visible items can be found in `https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/LazyListLayoutInfo#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.