https://kotlinlang.org logo
#compose
Title
# compose
h

Hitanshu Dhawan

12/18/2020, 5:13 PM
Hey, what’s the best way to smooth scroll to a particular position in
LazyColumnFor
I’m using alpha08, and I can find methods like
LazyListState.snapToItemIndex
but this is without smooth scrolling and the method
LazyListState.smoothScrollBy
doesn’t take the index. How to smooth scroll to a specific index? Does the API exists for this right now?
h

Hitanshu Dhawan

12/18/2020, 5:29 PM
Thanks for the quick response. I tried the
.scroll {}
API, and I’m not sure if I’m using it correctly. There is only one method for
ScrollScope
and that is
fun scrollBy(pixels: Float): Float
So, I tried this piece of code
Copy code
onClick = {
    coroutineScope.launch {
        state.scroll {
            state.snapToItemIndex(15)
        }
    }
}
But there is not scrolling or snapping to the index in this case, am I doing something wrong here?
a

Adam Powell

12/18/2020, 6:45 PM
You can animate the scroll yourself by using scrollBy and withFrameNanos or any of the other suspending animation APIs; you can check the current visible items in between frames
d

Dominaezzz

12/18/2020, 7:24 PM
There should be a
smoothScrollTo
available iirc.
m

Marcin Środa

12/22/2020, 11:13 AM
Agree with @Dominaezzz, right now the easiest way is to forget about smooth and use
snapToItemIndex
.
3 Views