Jetpack Compose – 无法创建嵌套的LazyColumns

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

Jetpack Compose - No possibilit to create a nested LazyColumns

问题

以下问题:我创建了一个Compose视图,应该显示一个项目列表(在将来的开发中还应该显示更多内容)。

我创建了以下视图:

  1. data class ItemHolder(
  2. val header: String,
  3. val subItems: List<String>,
  4. val footer: String
  5. )
  6. class MainActivity : ComponentActivity() {
  7. override fun onCreate(savedInstanceState: Bundle?) {
  8. super.onCreate(savedInstanceState)
  9. // 创建项目
  10. val items = (1..20).map { itemIndex ->
  11. ItemHolder(
  12. header = "第$itemIndex个标题",
  13. subItems = (1..30).map { subItemIndex ->
  14. "第$itemIndex个子项目的子项目 $subItemIndex"
  15. },
  16. footer = "第$itemIndex个页脚"
  17. )
  18. }
  19. setContent {
  20. Column(
  21. modifier = Modifier.verticalScroll(rememberScrollState())
  22. ) {
  23. Text(text = "项目:")
  24. ItemList(items = items)
  25. }
  26. }
  27. }
  28. }
  29. // 显示项目列表
  30. @Composable
  31. fun ItemList(items: List<ItemHolder>) {
  32. LazyColumn {
  33. items(items = items) {
  34. Item(item = it)
  35. }
  36. }
  37. }
  38. // 显示单个项目
  39. @Composable
  40. fun Item(item: ItemHolder) {
  41. var subItemsVisible by remember { mutableStateOf(false) }
  42. // 显示项目的标题
  43. Row {
  44. Text(text = item.header)
  45. Button(
  46. onClick = { subItemsVisible = !subItemsVisible },
  47. content = {
  48. Text(text = if (subItemsVisible) "隐藏" else "显示")
  49. }
  50. )
  51. }
  52. // 显示项目的子项目
  53. AnimatedVisibility(visible = subItemsVisible) {
  54. Column {
  55. for (subItem in item.subItems) {
  56. Text(text = subItem)
  57. }
  58. }
  59. }
  60. // 显示项目的页脚
  61. Text(text = item.footer)
  62. }

我发现问题是外部的Column(可以滚动)包含了包含实际项目的LazyColumn

我得到以下错误:

  1. 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:

  1. data class ItemHolder(
  2. val header: String,
  3. val subItems: List&lt;String&gt;,
  4. val footer: String
  5. )
  6. class MainActivity : ComponentActivity() {
  7. override fun onCreate(savedInstanceState: Bundle?) {
  8. super.onCreate(savedInstanceState)
  9. // Create items
  10. val items = (1..20).map { itemIndex -&gt;
  11. ItemHolder(
  12. header = &quot;Header of $itemIndex&quot;,
  13. subItems = (1..30).map { subItemIndex -&gt;
  14. &quot;Sub item $subItemIndex of $itemIndex&quot;
  15. },
  16. footer = &quot;Footer of $itemIndex&quot;
  17. )
  18. }
  19. setContent {
  20. Column(
  21. modifier = Modifier.verticalScroll(rememberScrollState())
  22. ) {
  23. Text(text = &quot;Items:&quot;)
  24. ItemList(items = items)
  25. }
  26. }
  27. }
  28. }
  29. // Displays the list of items
  30. @Composable
  31. fun ItemList(items: List&lt;ItemHolder&gt;) {
  32. LazyColumn {
  33. items(items = items) {
  34. Item(item = it)
  35. }
  36. }
  37. }
  38. // Displays a single item
  39. @Composable
  40. fun Item(item: ItemHolder) {
  41. var subItemsVisible by remember { mutableStateOf(false) }
  42. // Displays the header of the item
  43. Row {
  44. Text(text = item.header)
  45. Button(
  46. onClick = { subItemsVisible = !subItemsVisible },
  47. content = {
  48. Text(text = if (subItemsVisible) &quot;Hide&quot; else &quot;Show&quot;)
  49. }
  50. )
  51. }
  52. // Displays the sub items of the item
  53. AnimatedVisibility(visible = subItemsVisible) {
  54. Column {
  55. for (subItem in item.subItems) {
  56. Text(text = subItem)
  57. }
  58. }
  59. }
  60. // Displays the footer of the item
  61. Text(text = item.footer)
  62. }

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:

  1. 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

  1. data class ItemHolder(
  2. val header: String,
  3. val subItems: List<String>,
  4. val footer: String
  5. )
  6. class MainActivity : ComponentActivity() {
  7. override fun onCreate(savedInstanceState: Bundle?) {
  8. super.onCreate(savedInstanceState)
  9. val items = (1..4).map { itemIndex ->
  10. ItemHolder(
  11. header = "Header of $itemIndex",
  12. subItems = (1..30).map { subItemIndex ->
  13. "Sub item $subItemIndex of $itemIndex"
  14. },
  15. footer = "Footer of $itemIndex"
  16. )
  17. }
  18. setContent {
  19. LazyColumn(
  20. modifier = Modifier.padding(10.dp)
  21. ) {
  22. item {
  23. Text(text = "Items: Header", color = Color.Red, fontSize = 20.sp)
  24. Spacer(modifier = Modifier.height(20.dp))
  25. }
  26. items(items = items) {
  27. Item(item = it)
  28. }
  29. item {
  30. Spacer(modifier = Modifier.height(20.dp))
  31. Text(text = "Items: Footer", color = Color.Red, fontSize = 20.sp)
  32. Spacer(modifier = Modifier.height(20.dp))
  33. }
  34. items(items = items) {
  35. Item(item = it)
  36. }
  37. }
  38. }
  39. }
  40. }
  41. // Displays a single item
  42. @Composable
  43. fun Item(item: ItemHolder) {
  44. var subItemsVisible by remember { mutableStateOf(false) }
  45. // Displays the header of the item
  46. Row {
  47. Text(text = item.header)
  48. Button(
  49. onClick = { subItemsVisible = !subItemsVisible },
  50. content = {
  51. Text(text = if (subItemsVisible) "Hide" else "Show")
  52. }
  53. )
  54. }
  55. // Displays the sub items of the item
  56. AnimatedVisibility(visible = subItemsVisible) {
  57. Column {
  58. for (subItem in item.subItems) {
  59. Text(text = subItem)
  60. }
  61. }
  62. }
  63. // Displays the footer of the item
  64. Text(text = item.footer)
  65. }

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

  1. data class ItemHolder(
  2. val header: String,
  3. val subItems: List&lt;String&gt;,
  4. val footer: String
  5. )
  6. class MainActivity : ComponentActivity() {
  7. override fun onCreate(savedInstanceState: Bundle?) {
  8. super.onCreate(savedInstanceState)
  9. val items = (1..4).map { itemIndex -&gt;
  10. ItemHolder(
  11. header = &quot;Header of $itemIndex&quot;,
  12. subItems = (1..30).map { subItemIndex -&gt;
  13. &quot;Sub item $subItemIndex of $itemIndex&quot;
  14. },
  15. footer = &quot;Footer of $itemIndex&quot;
  16. )
  17. }
  18. setContent {
  19. LazyColumn(
  20. modifier = Modifier.padding(10.dp)
  21. ) {
  22. item {
  23. Text(text = &quot;Items: Header&quot;, color = Color.Red, fontSize = 20.sp)
  24. Spacer(modifier = Modifier.height(20.dp))
  25. }
  26. items(items = items) {
  27. Item(item = it)
  28. }
  29. item {
  30. Spacer(modifier = Modifier.height(20.dp))
  31. Text(text = &quot;Items: Footer&quot;, color = Color.Red, fontSize = 20.sp)
  32. Spacer(modifier = Modifier.height(20.dp))
  33. }
  34. items(items = items) {
  35. Item(item = it)
  36. }
  37. }
  38. }
  39. }
  40. }
  41. // Displays a single item
  42. @Composable
  43. fun Item(item: ItemHolder) {
  44. var subItemsVisible by remember { mutableStateOf(false) }
  45. // Displays the header of the item
  46. Row {
  47. Text(text = item.header)
  48. Button(
  49. onClick = { subItemsVisible = !subItemsVisible },
  50. content = {
  51. Text(text = if (subItemsVisible) &quot;Hide&quot; else &quot;Show&quot;)
  52. }
  53. )
  54. }
  55. // Displays the sub items of the item
  56. AnimatedVisibility(visible = subItemsVisible) {
  57. Column {
  58. for (subItem in item.subItems) {
  59. Text(text = subItem)
  60. }
  61. }
  62. }
  63. // Displays the footer of the item
  64. Text(text = item.footer)
  65. }

Jetpack Compose – 无法创建嵌套的LazyColumns

答案2

得分: 1

你可以简单地将LazyColumn替换为Column,如下所示:

  1. // 显示项目列表
  2. @Composable
  3. fun ItemList(items: List<ItemHolder>) {
  4. Column {
  5. items.indices.forEach { i ->
  6. Item(item = items[i])
  7. }
  8. }
  9. }
英文:

Simply you can replace LazyColumn by Column as follows:

  1. // Displays the list of items
  2. @Composable
  3. fun ItemList(items: List&lt;ItemHolder&gt;) {
  4. Column {
  5. items.indices.forEach { i -&gt;
  6. Item(item = items[i])
  7. }
  8. }
  9. }

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:

确定