我有一个 LazyColumn,我想在其中显示一个包含两列的水平行,因此我尝试使用 LazyHorizontalGrid 来实现它。但是我的应用程序因异常而崩溃 - IllegalArgumentException: LazyHorizontalGrid'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 内的 LazyHorizontalGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72661805/