Skip to content

Commit

Permalink
Fix LazyGridScrollbarAdapter to allow for items with unknown row/co…
Browse files Browse the repository at this point in the history
…lumn at any position in `visibleItemsInfo` (#1707)
  • Loading branch information
m-sasha authored Dec 9, 2024
1 parent 7d26900 commit 9f59218
Showing 1 changed file with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,24 +365,24 @@ internal class LazyGridScrollbarAdapter(
scrollState.scrollBy(value)
}

override fun averageVisibleLineSize(): Double{
private val lineIsKnown = { itemInfo: LazyGridItemInfo -> itemInfo.line() != unknownLine }

override fun averageVisibleLineSize(): Double {
val visibleItemsInfo = scrollState.layoutInfo.visibleItemsInfo
val indexOfFirstKnownLineItem = visibleItemsInfo.indexOfFirst { it.line() != unknownLine }
if (indexOfFirstKnownLineItem == -1)
return 0.0
val reallyVisibleItemsInfo = // Non-exiting visible items
visibleItemsInfo.subList(indexOfFirstKnownLineItem, visibleItemsInfo.size)

// Compute the size of the last line
val lastLine = reallyVisibleItemsInfo.last().line()
val lastLineSize = reallyVisibleItemsInfo

// First and last visible, non-exiting LazyGridItemInfo
val first = visibleItemsInfo.firstOrNull(lineIsKnown) ?: return 0.0
val last = visibleItemsInfo.last(lineIsKnown)

// Compute the size (e.g. height for vertical grid) of the last line
val lastLine = last.line()
val lastLineSize = visibleItemsInfo
.asReversed()
.asSequence()
.filter(lineIsKnown)
.takeWhile { it.line() == lastLine }
.maxOf { it.mainAxisSize() }

val first = reallyVisibleItemsInfo.first()
val last = reallyVisibleItemsInfo.last()
val lineCount = last.line() - first.line() + 1
val lineSpacingSum = (lineCount - 1) * lineSpacing
return (
Expand Down

0 comments on commit 9f59218

Please sign in to comment.