Can someone share insights on how to use `rememberLazyListState` with `LazyColumn` for a chat interf...
s
Can someone share insights on how to use
rememberLazyListState
with
LazyColumn
for a chat interface? JetChat in
compose-samples
still uses
ScrollableColumn
so it doesn’’t help much, especially in these areas: • How do we smooth scroll to show a new incoming message? Since we only have relative-based
smoothScrollBy
instead of
smoothScrollTo(0f)
(on
reverseLayout = true
), it’s hard to get to the right spot and
snapToIndex(0)
is unnatural • How do we get measurements of the specific message component sizes to make the right relative scrolling calculation? I’ve seen
chatScrollState.layoutInfo.visibleItemsInfo
but i’m not sure how to get updates when
layoutInfo
changes, sticking it inside
LaunchedEffect
doesn’t fire • How do we know when we are getting to the end of the
LazyColumn
so that we can fetch older messages and add them to the list? • In the case where new messages are coming in or old messages are being added, how do we maintain existing scroll position without the whole list jumping around?
cc @Andrey Kulikov
d
I also happen to be interested in this too and have implement some.
For the 3rd point you can use
onActive { onDispose {} }
on the oldest (or whatever you feel is right) item in the
LazyColumn
to trigger the fetch.
For the second point, try using a
snapshotFlow { .... }
to capture the changes.
For the first point, it's a relatively new feature so I'm not sure. In theory you can use keep calling
smoothScrollBy
until you're at the bottom of the list aha.
For the last point, when I get new messages I don't experience this jumping you describe.
s
@Dominaezzz good suggestion to support infinite scroll, i will try adding logic to
onActive
to detect when to fetch
@Dominaezzz are you using reverseLayout = true? If so, how do you show new messages that just arrived,
snapToIndex
or
smoothScrollBy
?
d
I am indeed using
reverseLayout = true
new messages tend to just show up without me having to scroll. Although if there's a tiny bit of scroll, it doesn't show up. Haven't gotten to that yet.
s
but do they smooth scroll or they just snap and appear?
d
It's a snap. I'm pretty sure most messengers do this though.
s
i don’t know about android, but there isn’t a single one on iOS that snaps
d
Fascinating. Slack snaps
Unless you're on iOS now lol.
s
most chat apps snap on desktop, but not on iOS
a
smoothScrollToItem
is still in progress, so not yet ready. we also don’t yet have a solid support for item animations, you can implement something on your own with tools like
AnimatedVisibility
, but not everything