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

Sam

12/10/2020, 11:25 PM
Does anyone have experience with the new suspend scrolling functions added in
alpha08
, specifically
lazyListState.smoothScrollBy
and
lazyListState.snapToItemIndex
? We have a chat application so these new functions are critical for starting at the bottom of the message list, as well as revealing new messages into view as they arrive. However, the api is quite cumbersome for these reasons: •
snapToItemIndex
takes an index, but it’s not very easy to know how many items are in a
LazyColumn
, because there could be group headers, arbitrary items etc and
firstVisibleItemIndex
doesn’t help much •
snapToItemIndex
will only align the item to the top of the `LazyColumn`’s viewport, which is unhelpful in scenarios of scrolling to the very bottom of the list. There’s also
scrollOffset
param but this requires every [variable height] item to be measured to calculate the offset, is this even possible? •
smoothScrollBy
seems to only take a relative offset, as you can imagine if 3 new messages come in within a chat interface, it’s a tough time to calculate everything needed to scroll to bottom, including the height of all new items as well as the current scroll position. I know some might suggest using
reverseScrollDirection
to avoid some of these challenges, but that leads to a bunch of tradeoffs down the line (e.g. maintaining scroll position when user is scrolled up, control over animation of new messages, infinite scroll older msgs, etc). Plus it’s not even available yet on
LazyColumn
!
a

Andrey Kulikov

12/10/2020, 11:59 PM
https://android-review.googlesource.com/c/platform/frameworks/support/+/1512306 should solve your use case. will be available in
alpha09
s

Sam

12/11/2020, 12:33 AM
thx @Andrey Kulikov but as mentioned,
reverseScrollDirection
helps with some aspects of a chat interface while making other (less-obvious) things more difficult. I will try it out, but the questions still remain about the scroll api
t

Timo Drick

12/11/2020, 9:16 AM
I think you could create a feature request to ask for a specific feature.
a

Andrey Kulikov

12/11/2020, 1:07 PM
for LazyColumn it will be not only
reverseScrollDirection
, but also
reverseLayout
. this means that we layout item in reversed order as well. and when you will do snapToItemIndex(0) it will position the first item at the bottom. yes, please file a bug if you think that there is still something not covered after
alpha09