How does one vertically center a specific item within a LazyColumn of dynamic, variable-height items?

49 Views Asked by At

Say I have a LazyColumn set up with an unknown number of items of different sizes. Like a list of messages within a conversation.

LazyColumn {
   items(…) { … }
}

How can one go about scrolling the lazy column to vertically center an item within the LazyColumn's visible content?

enter image description here

I know we can scroll by index with LazyListState.scrollToItem …but the variable height items make it hard to pre-calculate the required scroll offset. We can't know how much space all of the items will take without pre-composing the whole list.

0

There are 0 best solutions below