最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - How to create a LazyVerticalStaggeredGrid that items aligned to start for every even index? - Stack Overflow

programmeradmin0浏览0评论

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)
                    }
                }
            }
        }
    }
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论