英文:
Jetpack Compose - No possibilit to create a nested LazyColumns
问题
以下问题:我创建了一个Compose视图,应该显示一个项目列表(在将来的开发中还应该显示更多内容)。
我创建了以下视图:
data class ItemHolder(
val header: String,
val subItems: List<String>,
val footer: String
)
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 创建项目
val items = (1..20).map { itemIndex ->
ItemHolder(
header = "第$itemIndex个标题",
subItems = (1..30).map { subItemIndex ->
"第$itemIndex个子项目的子项目 $subItemIndex"
},
footer = "第$itemIndex个页脚"
)
}
setContent {
Column(
modifier = Modifier.verticalScroll(rememberScrollState())
) {
Text(text = "项目:")
ItemList(items = items)
}
}
}
}
// 显示项目列表
@Composable
fun ItemList(items: List<ItemHolder>) {
LazyColumn {
items(items = items) {
Item(item = it)
}
}
}
// 显示单个项目
@Composable
fun Item(item: ItemHolder) {
var subItemsVisible by remember { mutableStateOf(false) }
// 显示项目的标题
Row {
Text(text = item.header)
Button(
onClick = { subItemsVisible = !subItemsVisible },
content = {
Text(text = if (subItemsVisible) "隐藏" else "显示")
}
)
}
// 显示项目的子项目
AnimatedVisibility(visible = subItemsVisible) {
Column {
for (subItem in item.subItems) {
Text(text = subItem)
}
}
}
// 显示项目的页脚
Text(text = item.footer)
}
我发现问题是外部的Column
(可以滚动)包含了包含实际项目的LazyColumn
。
我得到以下错误:
java.lang.IllegalStateException: 垂直滚动组件的最大高度约束被测量为无穷大,这是不允许的。
我搜索了好几个小时,但没有找到适合我的问题的解决方案。
我该如何修复这个问题?
英文:
Following problem: I created a Compose View which should display a item list (it also should display more things in future development).
I created following view:
data class ItemHolder(
val header: String,
val subItems: List<String>,
val footer: String
)
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Create items
val items = (1..20).map { itemIndex ->
ItemHolder(
header = "Header of $itemIndex",
subItems = (1..30).map { subItemIndex ->
"Sub item $subItemIndex of $itemIndex"
},
footer = "Footer of $itemIndex"
)
}
setContent {
Column(
modifier = Modifier.verticalScroll(rememberScrollState())
) {
Text(text = "Items:")
ItemList(items = items)
}
}
}
}
// Displays the list of items
@Composable
fun ItemList(items: List<ItemHolder>) {
LazyColumn {
items(items = items) {
Item(item = it)
}
}
}
// Displays a single item
@Composable
fun Item(item: ItemHolder) {
var subItemsVisible by remember { mutableStateOf(false) }
// Displays the header of the item
Row {
Text(text = item.header)
Button(
onClick = { subItemsVisible = !subItemsVisible },
content = {
Text(text = if (subItemsVisible) "Hide" else "Show")
}
)
}
// Displays the sub items of the item
AnimatedVisibility(visible = subItemsVisible) {
Column {
for (subItem in item.subItems) {
Text(text = subItem)
}
}
}
// Displays the footer of the item
Text(text = item.footer)
}
I found out that the problem is, that the outer Column
(which is scrollable) contains the LazyColumn
which contains the actual items.
I get following error:
java.lang.IllegalStateException: Vertically scrollable component was measured with an infinity maximum height constraints, which is disallowed.
I was searching around for hours, but didn't find any suitable solution for my problem.
How can I fix this?
答案1
得分: 1
I think you have to remove modifier = Modifier.verticalScroll(rememberScrollState()) it will not work with nested lazy column
refer this link may be help you :https://proandroiddev.com/nested-scroll-with-jetpack-compose-9c3b054d2e12
I edit your code I hope it will help you
data class ItemHolder(
val header: String,
val subItems: List<String>,
val footer: String
)
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val items = (1..4).map { itemIndex ->
ItemHolder(
header = "Header of $itemIndex",
subItems = (1..30).map { subItemIndex ->
"Sub item $subItemIndex of $itemIndex"
},
footer = "Footer of $itemIndex"
)
}
setContent {
LazyColumn(
modifier = Modifier.padding(10.dp)
) {
item {
Text(text = "Items: Header", color = Color.Red, fontSize = 20.sp)
Spacer(modifier = Modifier.height(20.dp))
}
items(items = items) {
Item(item = it)
}
item {
Spacer(modifier = Modifier.height(20.dp))
Text(text = "Items: Footer", color = Color.Red, fontSize = 20.sp)
Spacer(modifier = Modifier.height(20.dp))
}
items(items = items) {
Item(item = it)
}
}
}
}
}
// Displays a single item
@Composable
fun Item(item: ItemHolder) {
var subItemsVisible by remember { mutableStateOf(false) }
// Displays the header of the item
Row {
Text(text = item.header)
Button(
onClick = { subItemsVisible = !subItemsVisible },
content = {
Text(text = if (subItemsVisible) "Hide" else "Show")
}
)
}
// Displays the sub items of the item
AnimatedVisibility(visible = subItemsVisible) {
Column {
for (subItem in item.subItems) {
Text(text = subItem)
}
}
}
// Displays the footer of the item
Text(text = item.footer)
}
英文:
I think you have to remove modifier = Modifier.verticalScroll(rememberScrollState()) it will not work with nested lazy column
refer this link may be help you :https://proandroiddev.com/nested-scroll-with-jetpack-compose-9c3b054d2e12
I edit your code I hope it will help you
data class ItemHolder(
val header: String,
val subItems: List<String>,
val footer: String
)
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val items = (1..4).map { itemIndex ->
ItemHolder(
header = "Header of $itemIndex",
subItems = (1..30).map { subItemIndex ->
"Sub item $subItemIndex of $itemIndex"
},
footer = "Footer of $itemIndex"
)
}
setContent {
LazyColumn(
modifier = Modifier.padding(10.dp)
) {
item {
Text(text = "Items: Header", color = Color.Red, fontSize = 20.sp)
Spacer(modifier = Modifier.height(20.dp))
}
items(items = items) {
Item(item = it)
}
item {
Spacer(modifier = Modifier.height(20.dp))
Text(text = "Items: Footer", color = Color.Red, fontSize = 20.sp)
Spacer(modifier = Modifier.height(20.dp))
}
items(items = items) {
Item(item = it)
}
}
}
}
}
// Displays a single item
@Composable
fun Item(item: ItemHolder) {
var subItemsVisible by remember { mutableStateOf(false) }
// Displays the header of the item
Row {
Text(text = item.header)
Button(
onClick = { subItemsVisible = !subItemsVisible },
content = {
Text(text = if (subItemsVisible) "Hide" else "Show")
}
)
}
// Displays the sub items of the item
AnimatedVisibility(visible = subItemsVisible) {
Column {
for (subItem in item.subItems) {
Text(text = subItem)
}
}
}
// Displays the footer of the item
Text(text = item.footer)
}
答案2
得分: 1
你可以简单地将LazyColumn替换为Column,如下所示:
// 显示项目列表
@Composable
fun ItemList(items: List<ItemHolder>) {
Column {
items.indices.forEach { i ->
Item(item = items[i])
}
}
}
英文:
Simply you can replace LazyColumn by Column as follows:
// Displays the list of items
@Composable
fun ItemList(items: List<ItemHolder>) {
Column {
items.indices.forEach { i ->
Item(item = items[i])
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论