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

Oliver.O

03/13/2023, 3:45 PM
Did anyone try to insert or remove 130 elements ahead of visible elements into a
LazyColumn
or other lazy list? Spoiler: It does not work, the list loses track of its visible element positions.
😮 1
👀 1
A reproducer and suggested fix can be found in https://issuetracker.google.com/issues/273025639
m

myanmarking

03/13/2023, 10:38 PM
Is they key unique and stable?
o

Oliver.O

03/13/2023, 10:48 PM
Yes, of course. The problem is a hard-coded sliding window in lazy layouts which remembers key-to-index mappings. If you insert a number of elements exceeding that window's size, the implementation fails to find the position. It then just stays at the previous visible elements' item indices, ignoring the actual shift of element positions. And it assumes that elements have been added/removed at the end of the list (instead of the beginning), moving the scrollbar in the wrong direction.
As you can see in the video, the right list always displays correctly, as it has a custom key-to-index map which does not suffer from the hard-coded limits. To fix this, I have mirrored the original implementations of
LazyColumn
and supporting code, adding an extra
keyToIndexMap
parameter. Both implementations in the video are driven by the exact same lists, keys and mappings.
5 Views