How can I remove the ugly snapping that occurs whe...
# compose
s
How can I remove the ugly snapping that occurs when a
LazyList
item (with
key
) is moved?
j
There is an animateItemPlacement modifier
s
That's not what I want. Let's say, I need to move the first item to the bottom:
Copy code
var items by remember {
    mutableStateOf(List (20) { it })
}

.clickable {
    items = items.toMutableList().apply {
        add(removeAt(0))
    }
}
This will cause the
LazyList
to snap to the bottom.
I just want to move the first item to the bottom AND keep the scroll offset where it currently is
j
Well you could try scrollToItem() for the second item
s
šŸ˜•
I'd rather not provide a key
c
Is expected behavior @Andrey Kulikov?
b
We have this same problem with RecyclerViews in the legacy view system. A list where the user can select how the items are sorted. When the sort order changes and the items are re-ordered (i.e. their position moves), it changes the scroll position such that the topmost completely visible item before the sort change remains visible after the sort change. This is not what the user wants/expects. If they are scrolled to the top of the list, and then click button to toggle the sort (descending vs ascending), they wind up scrolled all the way to the bottom. What the user expects (because weā€™ve gotten hundreds of complaints) is that the current scroll position should remain unchanged (i.e. if i was scrolled to the top and saw A,B,C .. then after changing the sort I should still be at the top and see Z,Y,X). Thereā€™s no ā€œbuilt-inā€ way to achieve the desired effect. Instead we have to write a bunch of code to record the current scroll position, sort the items, and then update the scroll position back to the recorded value. And itā€™s still not great, cause you get a visual ā€œjumpā€.
c
Great to know! I was just about to ask about RV (not as familiar myself unfortunately)
a
Hey! Yes, it is not too easy right now unfortunately. We track this feature request as part of this bug: https://issuetracker.google.com/issues/209652366
s
I ended up copying the relevant (short-ish)
androidx.compose.foundation.lazy
code so I could remove the snapping behavior (well, I would have done it anyway because of https://kotlinlang.slack.com/archives/CJLTWPH7S/p1654792126513769?thread_ts=1654780727.556209&cid=CJLTWPH7S).