【发布时间】:2021-06-10 10:53:08
【问题描述】:
当尝试将 LazyVerticalGrid 放入 可滚动 Column 中时,我收到以下错误:
java.lang.IllegalStateException: 在同一个嵌套可滚动 方向布局,如 LazyColumn 和 不允许使用 Column(Modifier.verticalScroll())。如果你想添加一个 项目列表前的标题请查看 LazyColumn 具有 DSL api 的组件,允许首先通过 item() 函数,然后通过 items() 获取项目列表。
我没有制作传统的列表,我只是有很多元素太大而无法在屏幕上显示。因此,我希望列滚动,以便我可以看到所有元素。这是我的代码:
@ExperimentalFoundationApi
@Composable
fun ProfileComposable(id: String?) {
val viewModel: ProfileViewModel = viewModel()
if (id != null) {
viewModel.getProfile(id)
val profile = viewModel.profile.value
val scrollState = rememberScrollState()
if (profile != null) {
Column(modifier = Modifier
.fillMaxWidth()
.fillMaxHeight()
.verticalScroll(scrollState)) {
Row() {
ProfilePic(profile.getImgUrl(), profile.name)
Column(Modifier.padding(16.dp)) {
ProfileName(profile.name)
Stats(profile.stats) // <--------------- the offending composable
}
}
Sprites(sprites = profile.sprites)
TextStat(profile.id.toString(), "Pokemon Number")
TextStat(profile.species.name, "Species")
TextStat(profile.types.joinToString { it.type.name }, "Types")
TextStat(profile.weight.toString(), "Weight")
TextStat(profile.forms.joinToString { it.name }, "Forms")
}
} else {
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
CircularProgressIndicator()
}
}
} else {
Text("Error")
}
}
Stats() 可组合包含导致错误的LazyVerticalGrid:
@ExperimentalFoundationApi
@Composable
fun Stats(stats: List<Stat>) {
LazyVerticalGrid(cells = GridCells.Fixed(2)) {
itemsIndexed(stats) { index, item ->
StatBox(stat = item)
}
}
}
我不想让网格滚动,我只想在可滚动的列中显示一个网格。
【问题讨论】:
-
你能检查一下这个问题是否对你有帮助吗? stackoverflow.com/questions/66908737/…
-
@nglauber 不幸的是没有。用
item{}和LazyColumn包裹它不起作用,我得到同样的错误。 -
@nglauber 是的,它不起作用,必须使用列和行组合。
-
@KesWalker 找到任何解决方案了吗?
-
@ArtemGarkusha 不。
标签: android kotlin android-jetpack-compose