gpt4 book ai didi

android - LazyColumn 内的 LazyHorizo​​ntalGrid

转载 作者:行者123 更新时间:2023-12-05 05:39:31 30 4
gpt4 key购买 nike

我有一个 LazyColumn,我想在其中显示一个包含两列的水平行,因此我尝试使用 LazyHorizo​​ntalGrid 来实现它。但是我的应用程序因异常而崩溃 - IllegalArgumentException: LazyHorizo​​ntalGrid's height should be bound by parent。下面是我正在使用的代码,任何人都可以帮助修复它或任何其他方法,通过它我可以使一行有两列。

@Composable
fun HomeItem1() {
Surface(modifier = Modifier.nestedScroll(rememberViewInteropNestedScrollConnection())) {
LazyColumn {
//other contents
item {
LazyHorizontalGrid(
rows = GridCells.Fixed(3),
horizontalArrangement = Arrangement.spacedBy(16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
items(arrayList.size) {
Text(arrayList[it])
}
}
}
}
}
}

最佳答案

所有你需要预先计算网格的高度。

    @Composable
fun HomeItem1() {
Surface(modifier = Modifier.nestedScroll(rememberViewInteropNestedScrollConnection())) {
LazyColumn {
//other contents
item {
LazyHorizontalGrid(
modifier = Modifier.height(176.dp), // itemHeight * rowCount + verticalSpacing * (rowCount - 1)
rows = GridCells.Fixed(3),
horizontalArrangement = Arrangement.spacedBy(16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
items(arrayList.size) {
Text(arrayList[it], modifier = Modifier.height(48.dp))
}
}
}
}
}
}

关于android - LazyColumn 内的 LazyHorizo​​ntalGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72661805/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com