Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to slow down AnimateScrollToItem in Jetpack Compose?

I have a lazy list that I am animating scroll to Item when clicking on a button.

onClick = {
  scope.launch  {
    lazyListState.animateScrollToItem(selectedIndex)
  }
}

Why is it that the animateScrollToItem is SO fast? Can I slow it down a little? I am not seeing anywhere I can add a animationSpec, and with animateScrollBy() I would need to pass in a float rather than an index - which I do not want.

like image 684
Android Dev Avatar asked Dec 19 '25 21:12

Android Dev


1 Answers

That's the only possible way for now. animateScrollBy() is not that bad by the way, all you need is to know the size of the lazy container item.

val itemSize = 50.dp
val density = LocalDensity.current
val itemSizePx = with(density) { itemSize.toPx() }
val itemsScrollCount = 150
coroutineScope.launch {
   lazyListState.animateScrollBy(
     value = itemSizePx * itemsScrollCount
     animationSpec = tween(durationMillis = 5000)
   )
}
like image 159
Akucuki Avatar answered Dec 21 '25 11:12

Akucuki