这个问题的原因是在 LazyColumn 中使用了具有相同 id 的多个项目。为了解决这个问题,我们需要在项目中指定唯一的 id。以下是一个示例代码,展示了如何在项目中使用一个值独特的 id:
@Composable
fun LazyColumnWithUniqueItem() {
val items = listOf("Item 1", "Item 2", "Item 3")
LazyColumn {
items(items, key = { it }) { item ->
Text(text = item)
}
}
}
在上面的代码中,我们通过指定它作为唯一的 id,让每个项目都有了一个独特的键。这将确保 LazyColumn 中不会出现重复的 id,从而解决了 IllegalArgumentException: Key was already used 的问题。