LazyColumn 内的 LazyHorizo​​ntalGrid

May*_*yur 7 android kotlin android-jetpack android-jetpack-compose

我有一个 LazyColumn,在其中我想显示一个包含两列的水平行,所以我尝试 LazyHorizo​​ntalGrid 来实现它。但我的应用程序崩溃了,但有例外 - 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])
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 4

您需要预先计算网格的高度。

    @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))
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

3500 次

最近记录:

2 年,1 月 前