Michael Paus
02/23/2023, 10:45 AManimateScrollToItem
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.
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.Alex Vanyo
02/23/2023, 6:35 PMindex
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 scrollMichael Paus
02/23/2023, 7:19 PManimateScrollToItemByKey(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.Alex Vanyo
02/25/2023, 12:55 AManimateScrollToItemByKey
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.Michael Paus
02/25/2023, 9:30 AM