Does anyone know how to add items to the top of a ...
# compose
n
Does anyone know how to add items to the top of a
LazyColumn
while keeping the current position e.g : when the list changes from (1,2,3,4,5) to (10,9,8,7,1,2,3,4,5), the first visible item remains 1?
t
Did you tried the reverse layout parameter of LazyColumn?
n
@Timo Drick this parameter is used to reverse the display of LazyColumn, which does not fit my question
t
Not exactly but maybe when you order it in reverse it would not scroll away?
Ok i did a short test and if you specify keys for your items the list will not scroll away. You do not need to reverse the list.
Copy code
LazyColumn {
    items(list, key = { it }) { item ->
        Text(item)
    }
}
Of course you need unique id for every item
This keys are also needed when you want to use animations when adding or removing items.
n
@Timo Drick it works! ty. ❤️ BTW, It turns out that I had been using the wrong syntax all along:
Copy code
items {
  key(it.id) { ...// content }
}
I'm not sure where I saw this syntax as equivalent to your previous code, but I just tested it and they are completely different.
s
The
key() {}
is there for situations where you’re not in a lazy list. it serves it’s purpose for other such scenarios. But when you’re in a lazy list you want to use the key in the items dsl indeed