gpt4 book ai didi

LazyVerticalGrid after delete item, the view is recomposed, but deleted item remain as 'ghost'(LazyVerticalGrid删除项目后,视图将重新组合,但已删除的项目仍将保留为“Ghost”)

转载 作者:bug小助手 更新时间:2023-10-28 13:14:24 24 4
gpt4 key购买 nike



I'm new to using jetpack compose and I have this problem. I have a LazyVerticalGrid which is populated from a list taken from a firestore database, when I delete an item through the application, the view is updated and the deleted item is not displayed, but if I click another item which is at the location of the deleted item, in the logs I see the references to the deleted item and if I click on the next item, I see that the previous one is selected. It is as if the 'ghost' of the deleted element remains and the indices are distorted.

我刚开始使用Jetpack Compose,我有这个问题。我有一个LazyVerticalGrid,它是从FireStore数据库中获取的列表填充的,当我通过应用程序删除一个项目时,视图会更新,删除的项目不会显示,但如果我单击删除项目所在位置的另一个项目,在日志中我会看到对删除项目的引用,如果我单击下一个项目,我会看到前一个项目被选中。这就好像被删除的元素的‘幽灵’仍然存在,索引被扭曲了。


In the BrandViewModel I have:

在我拥有的BrandViewModel中:


   val brands = storageService.brands

in StorageService:

在StorageService中:


   val brands: Flow<List<Brand>>

In StorageServiceImpl:

在StorageServiceImpl:


override val brands: Flow<List<Brand>>
get() =auth.currentUser.flatMapLatest { user ->
currentBrandCollection(user.id).snapshots().map { snapshot -> snapshot.toObjects() }
}

In the Brand Screen:

在品牌屏幕中:


 val brands by viewModel.brands.collectAsStateWithLifecycle(initialValue = emptyList())
..........

LazyVerticalGrid(
state = listGridState,
modifier = Modifier
.padding(vertical = 4.dp)
.padding(horizontal = 4.dp)
.background(MaterialTheme.colorScheme.background)
.padding(contentPadding),
columns = GridCells.Adaptive(minSize = 124.dp),
) {
itemsIndexed(brands) { index, brandItem->
..........

initial list

初始列表


Select item card

选择项目卡片


Delete item

删除项目


On tap on General Card, is select previous Card, Ferrarelle

轻触通用卡,是否选择上一张卡,Ferrarelle


更多回答
优秀答案推荐

Encountered the same problem, solved by providing to pointerInput inside items() {} the same key as for element of LazyVerticalGrid

遇到了相同的问题,解决方法是向Items()内的pointerInput提供与LazyVerticalGrid的元素相同的键


More detailed explanation: https://stackoverflow.com/a/68794149/19258573

更详细的解释:https://stackoverflow.com/a/68794149/19258573


LazyVerticalGrid(...) {
items(
items = bookmarks,
key = { bookmark ->
bookmark.id
}
) { curBookmark ->
HomeGridItem(
...
bookmarkId = curBookmark.id
...
}
}


@Composable
fun HomeGridItem(
...
) {
Column(
modifier = Modifier
.pointerInput(bookmarkId) {
detectTapGestures(
onTap = { onClick() },
onLongPress = {
onLongClick(globalCoordinates.value)
}
)
},
) { ... }


更多回答

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