Jetpack Compose – 无法创建嵌套的LazyColumns

huangapple go评论67阅读模式
英文:

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&lt;String&gt;,
    val footer: String
)

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Create items
        val items = (1..20).map { itemIndex -&gt;
            ItemHolder(
                header = &quot;Header of $itemIndex&quot;,
                subItems =  (1..30).map { subItemIndex -&gt;
                    &quot;Sub item $subItemIndex of $itemIndex&quot;
                },
                footer = &quot;Footer of $itemIndex&quot;
            )
        }

        setContent {
            Column(
                modifier = Modifier.verticalScroll(rememberScrollState())
            ) {
                Text(text = &quot;Items:&quot;)
                ItemList(items = items)
            }
        }
    }
}

// Displays the list of items
@Composable
fun ItemList(items: List&lt;ItemHolder&gt;) {
    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) &quot;Hide&quot; else &quot;Show&quot;)
            }
        )
    }

    // 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)
}

Jetpack Compose – 无法创建嵌套的LazyColumns

英文:

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&lt;String&gt;,
    val footer: String
)

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val items = (1..4).map { itemIndex -&gt;
            ItemHolder(
                header = &quot;Header of $itemIndex&quot;,
                subItems = (1..30).map { subItemIndex -&gt;
                    &quot;Sub item $subItemIndex of $itemIndex&quot;
                },
                footer = &quot;Footer of $itemIndex&quot;
            )
        }

        setContent {
            LazyColumn(
                modifier = Modifier.padding(10.dp)
            ) {
                item {
                    Text(text = &quot;Items: Header&quot;, 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 = &quot;Items: Footer&quot;, 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) &quot;Hide&quot; else &quot;Show&quot;)
            }
        )
    }

    // 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)
}

Jetpack Compose – 无法创建嵌套的LazyColumns

答案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&lt;ItemHolder&gt;) {
    Column {
        items.indices.forEach { i -&gt;
            Item(item = items[i])
        }
    }
}

huangapple
  • 本文由 发表于 2023年2月6日 19:11:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定