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

Anders Kielsholm

08/02/2021, 1:50 PM
Hi! 🙂 I have a usecase where I would like my LazyColumn to scroll to the top if I add a new item to the start of the list - but only if the list was scrolled to top before. Anyone knows how to do that? Think it's something like using a
LaunchedEffect
, but can't really wrap my head around it 🙂 I'm using keys so the list is in general keeping the same position when adding items 🙂
1
m

mcpiroman

08/03/2021, 8:31 AM
Copy code
val vertScrollState = rememberLazyListState()
LazyColumn(state = vertScrollState) 
vertScrollState.scrollToItem(0)
maybe? Assuming there is at least 1 item.
a

Anders Kielsholm

08/03/2021, 10:13 AM
Yeah, that would scroll the list to the top - but I'm uncertain how to do that when a new item is added to the top. There's examples on how to do it for a button click, but not based on several factors (list being at top before adding new item) 🙂
m

mcpiroman

08/03/2021, 10:17 AM
Ah, so it's about trigger. So maybe `remember`the size of your list at last compose and compare with the current one, then if the size is different (or just greater than) previous invoke the scroll?
Though that may miss if you remove and add an item quick enough...
a

Anders Kielsholm

08/03/2021, 10:30 AM
In my own case I actually have a single item at top that's shown/hidden based on a boolean, so I would be able to check that boolean. But yeah, my main concern is also about skipping a state since you can't be sure when recomposition happen and in what order as far as I understand it.
2 Views