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

Andre Classen

07/04/2021, 12:33 AM
Is there any chance to keep the scroll position when swapping the first visible item in an item keyed
LazyColumn
?
For example , if the list is scrolled to top and i swap the first item :
data[1] = data[0].also { data[0] = data[1] }
The scroll position should not anchored to data[0].
b

Berkeli Alashov

07/04/2021, 1:00 AM
j

Jorge Castillo

07/04/2021, 6:33 AM
That sounds like the way to go. Compose leverages positional memoization which means if identifies composables based on the hash of the call but also its position in the sources. In lazy lists each item is traditionally called in the same line and that can create non unique keys. Providing an explicit key can likely help to maintain unique identities and probably help on that scrolling issue
a

Andre Classen

07/04/2021, 9:26 AM
I`m using item keys :
Copy code
LazyColumn(state = listState) {
    items(data, { it.id }) { item ->
This causes the issue , swapping without item keys keeps the position. But thats not really an option as swapping items without keys dosent really work :/
6 Views