LazyGridPrefetchStrategy


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 LazyGridPrefetchScope.scheduleLinePrefetch to schedule prefetches from the onScroll and onVisibleItemsUpdated callbacks. If any of the returned PrefetchHandles no longer need to be prefetched, use LazyLayoutPrefetchState.PrefetchHandle.cancel to cancel the request.

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

Unit
NestedPrefetchScope.onNestedPrefetch(firstVisibleItemIndex: Int)

This function is deprecated. LazyGridPrefetchStrategy is deprecated in favor of LazyLayoutCacheWindow.

Cmn
Unit
LazyGridPrefetchScope.onScroll(
    delta: Float,
    layoutInfo: LazyGridLayoutInfo
)

This function is deprecated. LazyGridPrefetchStrategy is deprecated in favor of LazyLayoutCacheWindow.

Cmn
Unit

This function is deprecated. LazyGridPrefetchStrategy is deprecated in favor of LazyLayoutCacheWindow.

Cmn

Public properties

open PrefetchScheduler?

This property is deprecated. Customization of PrefetchScheduler is no longer supported.

Cmn

Public functions

NestedPrefetchScope.onNestedPrefetch

fun NestedPrefetchScope.onNestedPrefetch(firstVisibleItemIndex: Int): Unit

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 NestedPrefetchScope.schedulePrefetch 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, onNestedPrefetch 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, onNestedPrefetch should only request prefetch for children that it expects to actually be visible when this grid is scrolled into view.

Parameters
firstVisibleItemIndex: Int

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

fun LazyGridPrefetchScope.onScroll(
    delta: Float,
    layoutInfo: LazyGridLayoutInfo
): Unit

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 onVisibleItemsUpdated.

Parameters
delta: Float

the change in scroll direction. Delta < 0 indicates scrolling down while delta > 0 indicates scrolling up.

layoutInfo: LazyGridLayoutInfo

the current LazyGridLayoutInfo

LazyGridPrefetchScope.onVisibleItemsUpdated

fun LazyGridPrefetchScope.onVisibleItemsUpdated(
    layoutInfo: LazyGridLayoutInfo
): Unit

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

Parameters
layoutInfo: LazyGridLayoutInfo

the current LazyGridLayoutInfo. Info about the updated visible items can be found in LazyGridLayoutInfo.visibleItemsInfo.

Public properties

prefetchScheduler

open val prefetchSchedulerPrefetchScheduler?

A PrefetchScheduler implementation which will be used to execute prefetch requests for this strategy implementation. If null, the default PrefetchScheduler for the platform will be used.