How to prevent scrolling when adding new items into Lazy Column?
I am doing app chat, when load more on top. I get data from api into item first in lazy column but lazy column auto scrolls to top.
When the top item is shown (eg loading) on the screen and new items are added at the start of the list, then LazyColumn auto scrolls to the very top. What I am trying to achieve - replace the loading item with the last of the newly added items, so the user can smoothly scroll to the top. One of the important conditions - the list originally has to add items from top to bottom, thus the reverse layout does not fit.
s
Stylianos Gakis
11/11/2024, 7:21 AM
Not sure I understand why you can't use reverse layout here. Can you explain that a bit better?
Because I'm pretty sure that fixes your problem here
❤️ 1
t
Thanh nguyen
11/11/2024, 7:42 AM
In my understanding, reverseLayout reverses the order of the list. My approach is that when I reach index 0, I perform a "load more" operation to add 10 items to the top of the LazyColumn. However, I am encountering an issue where, after adding the new items, the LazyColumn automatically scrolls to the new index (which is 0), causing the screen to jitter.
I hope the
LazyColumn
to not automatically scroll to the new index (0) when adding 10 new items to the top.
s
Stylianos Gakis
11/11/2024, 7:46 AM
Yeah, and why is it a problem to use reverseLayout and load those 10 extra items when you reach the end of the list instead of the 0th item?
t
Thanh nguyen
11/11/2024, 7:53 AM
I hope to load more by scrolling from the bottom to the top, not by scrolling from the top to the bottom.
s
Stylianos Gakis
11/11/2024, 7:54 AM
Yeap, we do exactly that too using reverse layout. I am not sure what prevents you from doing so here.
What did you try so far which didn't work?
❤️ 1
t
Thanh nguyen
11/11/2024, 8:39 AM
Based on your suggestion, I was able to achieve it, and the scrolling is very smooth. Initially, I thought that reverseLayout only reversed the data, but it can also affect adding new items to the list. Thank you, I appreciate your guidance.