[英]LazyColumn inside Column with verticalScroll modifier throws exception
In one of my composables, a Lazycolumn
is nested inside a Column
composable.在我的一个可组合组件中,
Lazycolumn
嵌套在Column
可组合组件内。 I want to be able to scroll the entire Column
along with the Lazycolumn
.我希望能够与
Lazycolumn
一起滚动整个Column
。 But, specifying the verticalScroll
modifier
property on Column
results in the following exception causing the app to crash.但是,在
Column
上指定verticalScroll
modifier
属性会导致以下异常导致应用程序崩溃。 How can I fix this?我怎样才能解决这个问题?
Exception例外
java.lang.IllegalStateException: Vertically scrollable component was measured with an infinity maximum height constraints, which is disallowed. One of the common reasons is nesting layouts like LazyColumn and Column(Modifier.verticalScroll()).
Composable可组合的
Column(
modifier = Modifier
.fillMaxWidth()
.verticalScroll(rememberScrollState())
.padding(bottom = 100.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
LazyColumn(
modifier = Modifier
.fillMaxWidth()
) {
items(
items = allItems!!,
key = { item ->
item.id
}
) { item ->
ShoppingListScreenItem(
navController = navController,
item = item,
sharedViewModel = sharedViewModel
) { isChecked ->
scope.launch {
shoppingListScreenViewModel.changeItemChecked(item!!, isChecked)
}
}
}
}
...
Button(
modifier = Modifier.padding(vertical = 24.dp),
onClick = {
navController.navigate(NavScreens.AddItemScreen.route) {
popUpTo(NavScreens.AddItemScreen.route) {
inclusive = true
}
}
}
) {
Text("Go to add item screen")
}
}
This happens when you wish to measure your LazyColumn
with Constraints
with Constraints.Infinity
for the maxHeight which is not permitted as described in error log.当您希望使用
Constraints
测量您的LazyColumn
时,会发生这种情况, Constraints.Infinity
是不允许的,如错误日志中所述。 There should be a fixed height or you shouldn't have another Scrollable with same orientation.应该有一个固定的高度,或者你不应该有另一个具有相同方向的 Scrollable。
Column(
modifier = Modifier
// This is the cause
.verticalScroll(rememberScrollState())
) {
LazyColumn(
// and not having a Modifier that could return non-infinite max height contraint
modifier = Modifier
.fillMaxWidth()
) {
}
If you don't know exact height you can assign Modifier.weight(1f)
to LazyColumn.如果您不知道确切的高度,可以将
Modifier.weight(1f)
分配给 LazyColumn。
What i mean by measuring with with Constraints.Infinity is我用 Constraints.Infinity 测量的意思是
when you create a Layout in Compose you use当您在 Compose 中创建布局时,您使用
measurable.measure(constraints)
then you get child Composables as measurables and each one as然后你得到子 Composables 作为可测量的,每个作为
Modifier.fillMaxWidth()
These constraints change based on your size modifier.这些约束会根据您的大小修改器而变化。 When there is a scroll and you don't use any Size modifier Constraints return minHeight =0, maxHeight= some big number i don't recall.
当有滚动并且您不使用任何大小修饰符时,约束返回 minHeight =0,maxHeight= 一些我不记得的大数字。
Modifier.fillMaxWidth().weight(1f)
Modifier.fillMaxWidth().weight(1f)
Easiest way to check Constraints
with different Modifiers or with scroll is getting it from BoxWithConstraints
使用不同修饰符或滚动检查
Constraints
的最简单方法是从BoxWithConstraints
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.