Raed Ghazal
01/03/2025, 8:06 AMLizyColumn
data (on refresh), here is how I did it
LaunchedEffect(state.receipts) {
if (state.receipts.isNotEmpty()) {
listState.scrollToItem(0)
}
}
but it doesn't always work, its always called, but it doesn't scroll, and my guess here is that the LazyColumn
hasn't rendered/updated its content yet at this point so it scrolls in the old list, then updates the list with new data,
if I put a delay(500)
before calling scrollToItem
it works, but I don't wanna put some random value, that might not work for slower devices, any advise here?Raed Ghazal
01/03/2025, 8:10 AMold list:
receipt x <--- first visible item
receipt y
receipt z
new list after refresh:
receipt a
receipt x <--- still first visible item, I have to scroll up to see `a`
receipt y
receipt z
Louis
01/03/2025, 8:25 AMLouis
01/03/2025, 8:26 AMRaed Ghazal
01/03/2025, 8:26 AMRaed Ghazal
01/03/2025, 8:26 AMLouis
01/03/2025, 8:28 AMRaed Ghazal
01/03/2025, 8:46 AMscroll to item 0
even if we're at the middle
cuz if my call happens before rendering the new list, the empty item will become first and then stay first, and if my call happens after rendering the list, then it will scroll correctly, let me give it a tryRaed Ghazal
01/03/2025, 8:47 AMDmitry Strekha
01/03/2025, 9:46 AMrequestScrollToItem
https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/LazyListState#requestScrollToItem(kotlin.Int,kotlin.Int)
here's example of usage - https://x.com/and_kulikov/status/1775574132207358264Raed Ghazal
01/03/2025, 10:51 AMrequestScrollToItem
but it had the same issue, I guess what I did wrong was using LaunchEffect rather than SideEffect which runs AFTER the composition is done, while LaunchEffect actually ran at the beginning of the composition so it was getting invoked before the list gets the state changeefemoney
01/06/2025, 8:20 AM