简体   繁体   中英

Restore SwipeToDismiss LazyColumn Item to it's original state?

I am able to do SwipeToDismiss but I want to restore the swiped LazyColumn item back to its original state. I don't want to remove swiped item but want to restore it to its original state.

I am able to achieve this easily in RecyclerView by just calling notifyItemChanged() but can't figure out how to do this in LazyColumn .

Below is my code:

val dataList = remember{ mutableStateListOf<ListItem>()}
for(i in 0..100){
    dataList.add(ListItem("$i", "'$i' is the item number."))
}

LazyColumn(Modifier.fillMaxSize()){
    items(dataList, key = {it.id}){ item ->
        val dismissState = rememberDismissState(
            confirmStateChange = {
                if(it == DismissedToEnd || it == DismissedToStart){
                    Handler(Looper.getMainLooper()).postDelayed({
                        //dataList.remove(item)
                    }, 1000)
                }
                true
            }
        ) 
        SwipeToDismiss(
            state = dismissState,
            directions = setOf(StartToEnd, EndToStart),
            dismissThresholds = { direction ->
                FractionalThreshold(if (direction == StartToEnd || direction == EndToStart) 0.25f else 0.5f)
            },
            background = {
                val direction = dismissState.dismissDirection ?: return@SwipeToDismiss
                val color by animateColorAsState(
                    targetValue = when(dismissState.targetValue){
                        Default -> Color.LightGray
                        DismissedToEnd -> Color.Green
                        DismissedToStart -> Color.Red
                    }
                )
                val icon = when(direction){
                    StartToEnd -> Icons.Default.Done
                    EndToStart -> Icons.Default.Delete
                }
                val scale by animateFloatAsState(
                    if (dismissState.targetValue == Default) 0.8f else 1.2f
                )
                val alignment = when (direction) {
                    StartToEnd -> Alignment.CenterStart
                    EndToStart -> Alignment.CenterEnd
                }
                Box(modifier = Modifier
                    .fillMaxSize()
                    .background(color)
                    .padding(start = 12.dp, end = 12.dp),
                    contentAlignment = alignment
                ){
                    Icon(icon, contentDescription = "Icon", modifier = Modifier.scale(scale))
                }
            },
            dismissContent = {ItemScreen(dismissState = dismissState, item = item)}
        )
    }
}

You can wait currentValue to become non Default and reset the state:

if (dismissState.currentValue != DismissValue.Default) {
    LaunchedEffect(Unit) {
        dismissState.reset()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM