Horizontal staggered grid layout that composes and lays out only items currently visible on screen.
LazyHorizontalStaggeredGridSample
@Preview
@Composable
fun LazyHorizontalStaggeredGridSample() {
val itemsList = (0..5).toList()
val itemsIndexedList = listOf("A", "B", "C")
val itemModifier = Modifier.border(1.dp, Color.Blue).padding(16.dp).wrapContentSize()
LazyHorizontalStaggeredGrid(rows = StaggeredGridCells.Fixed(3)) {
items(itemsList) { Text("Item is $it", itemModifier) }
item { Text("Single item", itemModifier) }
itemsIndexed(itemsIndexedList) { index, item ->
Text("Item at index $index is $item", itemModifier)
}
}
}
LazyHorizontalStaggeredGridSpanSample
@Preview
@Composable
fun LazyHorizontalStaggeredGridSpanSample() {
val sections = (0 until 25).toList().chunked(5)
LazyHorizontalStaggeredGrid(
rows = StaggeredGridCells.Fixed(3),
verticalArrangement = Arrangement.spacedBy(16.dp),
horizontalItemSpacing = 16.dp,
) {
sections.forEachIndexed { index, items ->
item(span = StaggeredGridItemSpan.FullLine) {
Text(
"This is section $index",
Modifier.border(1.dp, Color.Gray).padding(16.dp).wrapContentSize(),
)
}
items(
items,
// not required as it is the default
span = { StaggeredGridItemSpan.SingleLane },
) {
Text("Item $it", Modifier.border(1.dp, Color.Blue).width(80.dp).wrapContentSize())
}
}
}
}