admin管理员组文章数量:1336613
I need to have a LazyVerticalStaggeredGrid
that aligns every even item to the left no matter of other which height other items have but while aligning depends height of last 2 items it aligns item to the right as
with
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)
}
}
}
I also tried using a custom arrangement with
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[i] = size
// } catch (e: Exception) {
// e.printStackTrace()
// }
// }
}
}
}
But it returns 2 items while there are 3 items.
Tried same thing with FlowRow which works as i needed when only there are 3 items
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)
}
}
}
but when item count is bigger it doesn't work as staggered grid should work obviously being a Row with 2 items and not aligning in staggered grid manner.
How to make custom arrangement or something else with LazyVerticalStaggeredGrid
so it aligns items starting from left no matter what height other items have?
Full code to play around or reproduce the issue
@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[i] = 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)
}
}
}
}
}
}
本文标签:
版权声明:本文标题:android - How to create a LazyVerticalStaggeredGrid that items aligned to start for every even index? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742409356a2469472.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论