How to scroll to a specific item of a lazy column?...
# compose
m
How to scroll to a specific item of a lazy column? I am creating a lazy column based on my model which describes a hierarchical structure with one selected item. (Think of a large book with thousands of pages subdivided into sections and sub-sections.) The column only unfolds and shows the elements which lead to the selected page. So far this is working nicely but then scrolling to the selected item seems to be strange. 1. How do you get at the index of an arbitrary item to be able to scroll to it? Is the below code the proposed way to get at it? 2. Where do you best place the below code (if it is correct) so that it is always executed after the composition because the index is only reliably available after it. 3. What rules does the
animateScrollToItem
follow? It does not seem to scroll the item to the top if this would result in blank space at the bottom of the column.
Copy code
val index = listState.layoutInfo.visibleItemsInfo.filter {
    it.key == myModelState.selectedReference?.refId }.firstOrNull()?.index
index?.let { listState.animateScrollToItem(index = it) }
All examples I have seen so far only seem to handle the trivial case that you want to scroll to the first item which is simply not a realistic use-case for me.
☝️ 1
1
a
You should be able to know the
index
based on the item(s) you are emitting into the
LazyColumn
. The easiest case is if you just have a single
items(myList)
, in which case the item index should match up with the index of that item in the
list
, but you might have to do some math if you have other
item
or
items
calls or conditional ones. I don’t think
animateScrollToItem
allows scrolling to a position that you couldn’t scroll to normally, so if you’re scrolling to the very last item in the list, it won’t go further than how far you could normally scroll
m
Well, that does not fit my use-case because I never have this complete list of items. That would be thousands. I create the items based on the users selection (in the model) via recursive calls of a method which retrieves the relevant items from a database. The problem is also not so much getting the index (the code above works perfectly) it is just that I felt that this is very cumbersome. If I have an API to register the items with a key then there should also be some API to retrieve them easily via this key. Ideally one would have a method
animateScrollToItemByKey(key)
. Keeping the items internally in a LinkedHashMap instead of a simple List might be useful. For the scrolling you seem to be right. I just checked that even manually I cannot scroll the last item up to the top. If you have enough items you can never create empty space in the column via scrolling. Probably this is just the way it is.
a
animateScrollToItemByKey
definitely sounds like a useful feature, I think https://issuetracker.google.com/issues/204723383 is the existing request if you want to follow it. I think the best alternative right now is to calculate what the resulting indices will be yourself based on the
item
and
items
being emitted. You might not have the complete list of items, but you should be able to calculate what index a specific item is going to be based on what you’re currently displaying.
m
Thanks for the link. That’s indeed exactly what I am looking for.
2386 Views