相关疑难解决方法(0)

在非惰性列中使用项目键

LazyColumn具有项目键,以便将项目的状态与唯一标识符而不是列表索引联系起来。有没有一种方法可以在像这样的非惰性列表中使用项目键?

Column {
  for (item in list) {
    Text(item)
  }
}
Run Code Online (Sandbox Code Playgroud)

我问的原因是因为我想实现SwipeToDismiss从列表中删除项目,这只有在将密钥传递给LazyColumn解决方案)时才有效,但是我的可忽略项目列表嵌套在 a 内部LazyColumn,并且我无法嵌套 aLazyColumn在 another 的 itemContent 块内部LazyColumn(不允许在同一方向布局中嵌套可滚动):

val items = listOf<String>(...)
val groups = items.groupBy { it.first() }

LazyColumn {
  items(groups, { /* key */ }) { (firstChar, group) ->
    // not allowed!
    LazyColumn {
      items(group, { /* key */ }) { item ->
        Text(item)
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我可以将调用items()本身包装在 for …

android-jetpack-compose

7
推荐指数
1
解决办法
4723
查看次数

标签 统计

android-jetpack-compose ×1