I have a LazyColumn with custom keys Now after i r...
# compose
v
I have a LazyColumn with custom keys Now after i refresh the PagingData using
LazyPagingitems.refresh()
, the scroll state is maintained How can i scroll to top if new items are added in top ?? I am not doing backward pagination (prepend)
a
Just modify the scroll state on receiving new data, you can use a LaunchedEffect to watch the data and trigger a scroll
v
Do you mean observing the refresh LoadState and scrolling to top?
Wont that mess up scroll state when navigating back to this screen
a
Well, you have to decide on conditions when you should scroll to top (after refresh that brings new data) and check for those
That won’t mess up scroll state on navigating back
v
no way to check If refresh call brought new data
On returning back LaunchedEffect will run again, all states reset and hence it will lose scroll state
m
Use LaunchedEffect with loadState.refresh as key when loadState.refresh is true then scroll to the top of the list
v
@Mahmoo if I navigate to some other screen and come back LaunchedEffect will run again, loadState.refresh is still NotLoading in that case and hence it will lose scroll state
a
A super simple way would be to store the current item count in a rememberSaveable and only scroll if the itemcount increased. I’m not saying this is the solution you should take, a way better way would be to put the scroll top logic into the viewmodel and expose a flow to trigger the scrolling. Just pointing out ways to achieve this.
We can huddle and you can show me the code
m
https://stackoverflow.com/a/71123229 See if saving list state helps in your case
163 Views