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

ste

08/11/2022, 3:37 PM
How can I remove the ugly snapping that occurs when a
LazyList
item (with
key
) is moved?
j

John Nichol

08/11/2022, 3:39 PM
There is an animateItemPlacement modifier
s

ste

08/11/2022, 3:42 PM
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

John Nichol

08/11/2022, 3:45 PM
Well you could try scrollToItem() for the second item
s

ste

08/11/2022, 3:46 PM
😕
I'd rather not provide a key
c

Chris Sinco [G]

08/11/2022, 5:41 PM
Is expected behavior @Andrey Kulikov?
b

Bradleycorn

08/11/2022, 5:52 PM
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

Chris Sinco [G]

08/11/2022, 5:58 PM
Great to know! I was just about to ask about RV (not as familiar myself unfortunately)
a

Andrey Kulikov

08/15/2022, 4:31 PM
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

ste

08/16/2022, 9:17 PM
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).
53 Views