android SwipeToDismess -滑动时,SwipeToDismiss会覆盖下一个列表项,如何修复此问题?

fykwrbwg  于 2023-04-28  发布在  Android
关注(0)|答案(1)|浏览(115)

我正在使用Jetpack Compose中的SwipeToDismiss从列表中删除项目。问题是,每当你滑动一个项目时,它会删除滑动的项目,但滑动的屏幕不会消失。
此图像显示了问题。

它将覆盖其下方的项目。
下面是我的SwipeToDismiss代码

val dismissState = rememberDismissState(
    confirmStateChange = {
        if (it == DismissValue.DismissedToStart) {
            storedItemsViewModel.deleteOne(sentence)
        }
        true
    }
)
val direction = dismissState.dismissDirection
SwipeToDismiss(
    state = dismissState,
    /***  create dismiss alert Background */
    directions = setOf(
        DismissDirection.EndToStart
    ),
    dismissThresholds = { /*direction ->*/
        FractionalThreshold(if (direction == DismissDirection.EndToStart) 0.1f else 0.5f)
        FractionalThreshold(0.2f)
    },
    background = {
        val color = when (dismissState.dismissDirection) {
            DismissDirection.StartToEnd -> Color.Green
            DismissDirection.EndToStart -> Color.Red
            null -> Color.Transparent
        }
        val alignment = Alignment.CenterEnd
        val icon = Icons.Default.Delete
        val scale by animateFloatAsState(
            //this scales the icon
            if (dismissState.targetValue == DismissValue.Default) 0.75f else 1f
        )
        Box(
            Modifier
                .fillMaxSize()
                .background(color)
                .padding(horizontal = Dp(20f)),
            contentAlignment = alignment
        ) {
            Icon(
                icon,
                contentDescription = "Delete Icon",
                modifier = Modifier.scale(scale)
            )
        }
    },

    /**** Dismiss Content */
    dismissContent = {
        Card(elevation = animateDpAsState( if (dismissState.dismissDirection != null) 4.dp else 0.dp).value) {
            FavoriteItem(sentence, word, grammar)
        }
    },
)
8i9zcol2

8i9zcol21#

您必须为lazyItems提供唯一的密钥。

LazyColumn(modifier = Modifier
                    .fillMaxWidth()) {
                    items(
                        cartRowList,
                        key ={ cartRow -> cartRow.id},
                        itemContent = {  it ->
                            val currentItem by rememberUpdatedState(it.item)
                            val dismissState = rememberDismissState(
                                confirmStateChange = {
                                    CartDataProcessing.deleteItem(context,currentItem!!)
                                    true
                                }

...

相关问题