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

Michael Paus

02/23/2023, 10:45 AM
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

Alex Vanyo

02/23/2023, 6:35 PM
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

Michael Paus

02/23/2023, 7:19 PM
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

Alex Vanyo

02/25/2023, 12:55 AM
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

Michael Paus

02/25/2023, 9:30 AM
Thanks for the link. That’s indeed exactly what I am looking for.
1839 Views