https://kotlinlang.org logo
Title
t

Tgo1014

04/05/2022, 9:34 AM
Is there a way to control the “offset” of a
LazyRow
? The idea would be to manually controle the
LazyRow
position to sync the scroll when dragging a pager
a

Albert Chang

04/05/2022, 9:37 AM
There is
scrollToItem()
.
t

Tgo1014

04/05/2022, 9:42 AM
With
scrollToItem()
it moves directly but the idea is to sync the drags to move together
a

Albert Chang

04/05/2022, 9:46 AM
That's what you use to control the offset. If you want to sync it with another one then put it in a
LaunchedEffect
.
t

Tgo1014

04/05/2022, 10:19 AM
But then in this case I’ll move just after the item is selected in the other Row, the idea is when you scroll one the other moves together even in between the items. This is possible with the
Row
using a
Modifier.offset
but this doesn’t work with a
LazyRow
.
a

Albert Chang

04/05/2022, 10:23 AM
Why do you think it’s not possible to sync the offset when scrolling the other one?
t

Tgo1014

04/05/2022, 10:26 AM
I guess I’m not explaining correctly the idea, let me get a video of it working with
Row
now.
So the bottom is a pager which provides the offset and the top one is a row. Using
Modifier.offset
we can move the items according to the pager offset but this via modifier doesn’t work for
LazyRow
. Any idea how to do it?
a

Albert Chang

04/05/2022, 10:33 AM
I understand what you want and that’s why I told you to use
LaunchedEffect
. Something like this should work:
LaunchedEffect(pagerState.currentPage, pagerState.currentPageOffest) {
    lazyListState.scrollToItem(pagerState.currentPage, pagerState.currentPageOffest)
}
t

Tgo1014

04/05/2022, 10:36 AM
But the second param of
scrollToItem
is
Int
but
pagerState.currentPageOffest
is Float, so a 0.5 offset would be 0 as int
a

Albert Chang

04/05/2022, 10:37 AM
Then round it to an int
I believe you need to do some calculation anyway as items in the LazyRow and the Pager are not of the same width.
t

Tgo1014

04/05/2022, 10:42 AM
You were correct. Adapting your idea a bit to the code worked fine. The final code looks like this:
LaunchedEffect(state.currentPageOffset) {
    scrollState.scrollToItem(
        state.currentPage, 
        state.currentPageOffset.times(itemWidthPx).toInt()))
}
Thanks a lot for the help 🙌🏽
👍 2