Skip to content

Commit

Permalink
Stop scrolling and trigger layout request when item changes affect th…
Browse files Browse the repository at this point in the history
…e layout structure
  • Loading branch information
rubensousa committed Jul 8, 2024
1 parent a33c8d6 commit 7243741
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,40 @@ internal class PivotLayout(
fun onItemsAdded(positionStart: Int, itemCount: Int) {
itemChanges.insertionPosition = positionStart
itemChanges.insertionItemCount = itemCount
onItemsChanged()
}

fun onItemsRemoved(positionStart: Int, itemCount: Int) {
itemChanges.removalPosition = positionStart
itemChanges.removalItemCount = itemCount
onItemsChanged()
}

fun onItemsMoved(from: Int, to: Int, itemCount: Int) {
itemChanges.moveFromPosition = from
itemChanges.moveToPosition = to
itemChanges.moveItemCount = itemCount
onItemsChanged()
}

private fun onItemsChanged() {
if (!layoutInfo.isScrolling) {
return
}
val firstPos = layoutInfo.findFirstAddedPosition()
val lastPos = layoutInfo.findLastAddedPosition()
val changesOutOfBounds = if (!layoutInfo.shouldReverseLayout()) {
itemChanges.isOutOfBounds(firstPos, lastPos)
} else {
itemChanges.isOutOfBounds(lastPos, firstPos)
}
if (changesOutOfBounds) {
return
}
layoutInfo.getRecyclerView()?.apply {
stopScroll()
requestLayout()
}
}

fun setOnChildLaidOutListener(listener: OnChildLaidOutListener?) {
Expand Down

0 comments on commit 7243741

Please sign in to comment.