LazyGridItemScope


Receiver scope being used by the item content parameter of LazyVerticalGrid.

Summary

Public functions

Modifier
Modifier.animateItem(
    fadeInSpec: FiniteAnimationSpec<Float>?,
    placementSpec: FiniteAnimationSpec<IntOffset>?,
    fadeOutSpec: FiniteAnimationSpec<Float>?
)

This modifier animates the item appearance (fade in), disappearance (fade out) and placement changes (such as an item reordering).

Cmn

Public properties

Int

Returns the number of cross-axis cells resolved from GridCells.

Cmn

Public functions

Modifier.animateItem

fun Modifier.animateItem(
    fadeInSpec: FiniteAnimationSpec<Float>? = spring(stiffness = Spring.StiffnessMediumLow),
    placementSpec: FiniteAnimationSpec<IntOffset>? = spring( stiffness = Spring.StiffnessMediumLow, visibilityThreshold = IntOffset.VisibilityThreshold, ),
    fadeOutSpec: FiniteAnimationSpec<Float>? = spring(stiffness = Spring.StiffnessMediumLow)
): Modifier

This modifier animates the item appearance (fade in), disappearance (fade out) and placement changes (such as an item reordering).

You should also provide a key via LazyGridScope.item/LazyGridScope.items for this modifier to enable animations.

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier

var list by remember { mutableStateOf(listOf("A", "B", "C")) }
Column {
    Button(onClick = { list = list + "D" }) { Text("Add new item") }
    Button(onClick = { list = list.shuffled() }) { Text("Shuffle") }
    LazyVerticalGrid(columns = GridCells.Fixed(1)) {
        items(list, key = { it }) { Text("Item $it", Modifier.animateItem()) }
    }
}
Parameters
fadeInSpec: FiniteAnimationSpec<Float>? = spring(stiffness = Spring.StiffnessMediumLow)

an animation specs to use for animating the item appearance. When null is provided the item will be appearing without animations.

placementSpec: FiniteAnimationSpec<IntOffset>? = spring( stiffness = Spring.StiffnessMediumLow, visibilityThreshold = IntOffset.VisibilityThreshold, )

an animation specs that will be used to animate the item placement. Aside from item reordering all other position changes caused by events like arrangement or alignment changes will also be animated. When null is provided no animations will happen.

fadeOutSpec: FiniteAnimationSpec<Float>? = spring(stiffness = Spring.StiffnessMediumLow)

an animation specs to use for animating the item disappearance. When null is provided the item will be disappearance without animations.

Public properties

crossAxisCellCount

val crossAxisCellCount: Int

Returns the number of cross-axis cells resolved from GridCells.

Equals the number of columns in LazyVerticalGrid or rows in LazyHorizontalGrid, matching LazyGridItemSpanScope.maxLineSpan and LazyGridLayoutInfo.maxSpan. For example, in a 3-column LazyVerticalGrid, this value is 3 for every item regardless of its GridItemSpan.

LazyVerticalGrid and LazyHorizontalGrid resolve GridCells constraints before subcomposing items, so this property returns the resolved count (always at least 1) on the first frame.

Backed by snapshot androidx.compose.runtime.State: reading this property during item composition, layout (such as androidx.compose.ui.layout.layout), or draw (such as androidx.compose.ui.draw.drawBehind) invalidates only that phase when the resolved count changes (for example, when resizing a GridCells.Adaptive grid).

import androidx.compose.animation.core.tween
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

LazyVerticalGrid(
    columns = GridCells.Adaptive(minSize = 180.dp),
    horizontalArrangement = Arrangement.spacedBy(8.dp),
    verticalArrangement = Arrangement.spacedBy(8.dp),
) {
    items(20) { index ->
        // Customize item UI based on the number of columns (crossAxisCellCount)
        if (crossAxisCellCount == 1) {
            // Single column: show full-width detailed row
            Row(
                modifier = Modifier.fillMaxWidth().border(1.dp, Color.Gray).padding(16.dp),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically,
            ) {
                Text("Item #$index")
                Text("Detailed row")
            }
        } else {
            // Multi-column: show compact tile
            Box(
                modifier =
                    Modifier.fillMaxWidth()
                        .height(80.dp)
                        .border(1.dp, Color.Blue)
                        .padding(8.dp),
                contentAlignment = Alignment.Center,
            ) {
                Text("Item #$index ($crossAxisCellCount cols)")
            }
        }
    }
}