gpt4 book ai didi

android - LazyColumn - 项目关键参数用途?

转载 作者:行者123 更新时间:2023-12-04 23:47:04 31 4
gpt4 key购买 nike

谁能解释一下 的主要目的是什么? ' key ' LazyListScope 的 items/itemsIndexed 函数内的参数?如果我们指定该参数,我们会得到什么或没有得到什么?我不确定我是否了解与此参数相关的官方文档:

key - a factory of stable and unique keys representing the item. Usingthe same key for multiple items in the list is not allowed. Type ofthe key should be saveable via Bundle on Android. If null is passedthe position in the list will represent the key. When you specify thekey the scroll position will be maintained based on the key, whichmeans if you add/remove items before the current visible item the itemwith the given key will be kept as the first visible one.

最佳答案

我认为 official doc 提供了最佳答案:

By default, each item's state is keyed against the position of the item in the list. However, this can cause issues if the data set changes, since items which change position effectively lose any remembered state. If you imagine the scenario of LazyRow within a LazyColumn, if the row changes item position, the user would then lose their scroll position within the row.


To combat this, you can provide a stable and unique key for each item, providing a block to the key parameter. Providing a stable key enables item state to be consistent across data-set changes:

@Composable
fun MessageList(messages: List<Message>) {
LazyColumn {
items(
items = messages,
key = { message ->
// Return a stable + unique key for the item
message.id
}
) { message ->
MessageRow(message)
}
}
}

关于android - LazyColumn - 项目关键参数用途?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68790215/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com