Is there a way to control the “offset” of a `LazyR...
# compose
t
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
There is
scrollToItem()
.
t
With
scrollToItem()
it moves directly but the idea is to sync the drags to move together
a
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
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
Why do you think it’s not possible to sync the offset when scrolling the other one?
t
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
I understand what you want and that’s why I told you to use
LaunchedEffect
. Something like this should work:
Copy code
LaunchedEffect(pagerState.currentPage, pagerState.currentPageOffest) {
    lazyListState.scrollToItem(pagerState.currentPage, pagerState.currentPageOffest)
}
t
But the second param of
scrollToItem
is
Int
but
pagerState.currentPageOffest
is Float, so a 0.5 offset would be 0 as int
a
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
You were correct. Adapting your idea a bit to the code worked fine. The final code looks like this:
Copy code
LaunchedEffect(state.currentPageOffset) {
    scrollState.scrollToItem(
        state.currentPage, 
        state.currentPageOffset.times(itemWidthPx).toInt()))
}
Thanks a lot for the help 🙌🏽
👍 2