I want to scroll a `LazyRow` so that the element ...
# compose
s
I want to scroll a
LazyRow
so that the element is centered.
LazyListState.animateScrollToItem()
takes an
index
and a
scrollOffset
as arguments. Per default it scrolls the list so that the item is the first one. If a positive
scrollOffset
is given it scrolls further, making the item go off scren. So I tried to give a negative
scrollOffset
and calculate what I need so that the element is in center position. The problem is: Negative Offsets are not allowed. 😞 How can I scroll to an index that it's at the center of the list?
👀 3
j
There is a feature request https://issuetracker.google.com/184252837 to allow for negative offset
without that the only way I can think of doing it is if your items are all the same size so you could scroll the appropriate other item to the top of the view - hacky I know
s
Thank you for pointing me to the feature request. Unfortunately they are not all the same size. Nevertheless I need to hack that I guess.
@John Nichol In SwiftUI you can provide an
anchor
like
leading
,
center
and
trailing
to the
scrollTo
function and it works out-of-the-box. I wish I had the same with Jetpack Compose.
@John Nichol I wasn't successful to find a reliable relatively quick hack. This looks like a lot of work to me to do that in a proper way. I created an issue for the Jetpack Compose team and we will see if they manage to match functionality with SwiftUI here. https://issuetracker.google.com/issues/200374702
785 Views