I'm looking for a way to synchronise scroll betwee...
# compose
j
I'm looking for a way to synchronise scroll between multiple
LazyColumns
. All columns should receive the same scroll delta, regardless of where the user input came from. Is
nestedScroll()
the right thing to use?
s
Are those
LazyColumns
nested (I don't think so)? If not, you could try to share a
LazyListState
(
rememberLazyListState
)
j
They are not nested. I tried sharing the
LazyListState
that did not work. Only one list is scrolling then. Sharing a
ScrollState
between multiple scrollable
Columns
works on the other hand. Unfortunately I have to use
LazyColumns
as the lists contain many items.
s
Ok, I got it working (kind of) like this:
Copy code
val state = rememberLazyListState()
val state2 = remember(state.firstVisibleItemScrollOffset) { state }
👀 1
a
Do you have a visual mock of the UX you're trying to build?
j
Attached you'll find a screenshot + video of my use case. Basically we're trying to mimic the google android calendar week-view. FYI: We use
HorizontalPager
for navigating between weeks. PS: We already have two apps 100% driven by compose running in production and we love it :)
a
glad you're enjoying it 🙂
the video leaves me with two questions: 1) why is this two separate scrolling containers instead of one? 2) why use LazyColumn here when the data set is so small? (only a single day?)
j
1) The time column should be sticky 2) When using Column instead of LazyColumn I experienced lags when the user selects a day, since the entire selected day column will change its background, which forces the entire page to be recomposed. I assumed that would be the reason for the lag. Do you suggest I should rather try to find a way to avoid recomposition of the whole page, instead of trying to sync LazyLists?
a
I'd start there, yes
j
Thank you for your thoughts Adam! I refactored the composable hierarchy which was quite complex. Also instead of using a column with multiple rows I now use a row with multiple columns. The
ScrollState
is shared, the lists are synced and no lags.
136 Views