Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
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)}
        )
    }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

According to Thinking in Compose, composable function should be free of side effects - you shouldn't directly reset the state in the composable scope. For such situations you need to use one of special side effect functions, more info can be found in side-effects documentation.

Recomposition can happen many times, up to once a frame during animation, and not using side effect functions will lead to multiple calls, which can cause animation problems.

As DismissState.reset() is a suspend function, LaunchedEffect fits perfectly here: it's already running on a coroutine scope.

if (dismissState.currentValue != DismissValue.Default) {
    LaunchedEffect(Unit) {
        dismissState.reset()
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!