
с
LazyVerticalStaggeredGrid(
contentPadding = PaddingValues(16.dp),
columns = StaggeredGridCells.Fixed(2),
verticalItemSpacing = 8.dp,
// horizontalArrangement = arrangement
) {
items(3) {
val height = if (it == 0) {
240.dp
} else 150.dp
Box(
modifier = Modifier
.size(itemWidth, height)
.background(Color.Red, RoundedCornerShape(16.dp)),
contentAlignment = Alignment.Center
) {
Text("Item: $it", fontSize = 30.sp, color = Color.White)
}
}
}
Я также пробовал использовать специальную компоновку с
val arrangement = remember {
object : Arrangement.Horizontal {
var size = 0
override fun Density.arrange(
totalSize: Int,
sizes: IntArray,
layoutDirection: LayoutDirection,
outPositions: IntArray,
) {
println(
"total size: $totalSize," +
" sizes: ${sizes.size}, " +
"outPositions: ${outPositions.size}"
)
// sizes.forEachIndexed { index, i ->
// size = if (index % 2 == 0) 0 else i
// try {
// outPositions = size
// } catch (e: Exception) {
// e.printStackTrace()
// }
// }
}
}
}
Но он возвращает 2 элемента, хотя есть 3 элемента.
Пробовал то же самое с FlowRow, который работает так, как мне нужно, когда есть только 3 элемента< /p>

FlowRow(
maxItemsInEachRow = 2,
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
repeat(3){
val height = if (it == 0) {
240.dp
} else 150.dp
Box(
modifier = Modifier
.size(itemWidth, height)
.background(Color.Red, RoundedCornerShape(16.dp)),
contentAlignment = Alignment.Center
) {
Text("Item: $it", fontSize = 30.sp, color = Color.White)
}
}
}
но когда количество элементов больше, это не работает, поскольку шахматная сетка должна работать, очевидно, как строка с двумя элементами, а не выравниваться в шахматном порядке.

Как сделать индивидуальную аранжировку или что-то в этом роде else с помощью LazyVerticalStaggeredGrid, чтобы он выравнивал элементы, начиная слева, независимо от высоты других элементов?
Полный код для решения или воспроизведения проблемы
@Preview
@Composable
fun StaggeredGridAlignTest() {
BoxWithConstraints(
modifier = Modifier.padding(16.dp)
) {
val itemWidth = maxWidth / 2 - 8.dp
val arrangement = remember {
object : Arrangement.Horizontal {
var size = 0
override fun Density.arrange(
totalSize: Int,
sizes: IntArray,
layoutDirection: LayoutDirection,
outPositions: IntArray,
) {
println(
"total size: $totalSize," +
" sizes: ${sizes.size}, " +
"outPositions: ${outPositions.size}"
)
// sizes.forEachIndexed { index, i ->
// size = if (index % 2 == 0) 0 else i
// try {
// outPositions = size
// } catch (e: Exception) {
// e.printStackTrace()
// }
// }
}
}
}
Column(
modifier = Modifier.fillMaxSize()
) {
LazyVerticalStaggeredGrid(
columns = StaggeredGridCells.Fixed(2),
verticalItemSpacing = 8.dp,
horizontalArrangement = arrangement
) {
items(3) {
val height = if (it == 0) {
240.dp
} else 150.dp
Box(
modifier = Modifier
.size(itemWidth, height)
.background(Color.Red, RoundedCornerShape(16.dp)),
contentAlignment = Alignment.Center
) {
Text("Item: $it", fontSize = 30.sp, color = Color.White)
}
}
}
FlowRow(
maxItemsInEachRow = 2,
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
repeat(3) {
val height = if (it == 0) {
240.dp
} else 150.dp
Box(
modifier = Modifier
.size(itemWidth, height)
.background(Color.Red, RoundedCornerShape(16.dp)),
contentAlignment = Alignment.Center
) {
Text("Item: $it", fontSize = 30.sp, color = Color.White)
}
}
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... -for-every
Мобильная версия