From 7243741eea239e31cd5e59dea7b74e6b7170c50e Mon Sep 17 00:00:00 2001 From: Ruben Sousa Date: Tue, 9 Jul 2024 00:01:32 +0200 Subject: [PATCH] Stop scrolling and trigger layout request when item changes affect the layout structure --- .../layoutmanager/layout/PivotLayout.kt | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/dpadrecyclerview/src/main/java/com/rubensousa/dpadrecyclerview/layoutmanager/layout/PivotLayout.kt b/dpadrecyclerview/src/main/java/com/rubensousa/dpadrecyclerview/layoutmanager/layout/PivotLayout.kt index a8b9ee1c..d07de77a 100644 --- a/dpadrecyclerview/src/main/java/com/rubensousa/dpadrecyclerview/layoutmanager/layout/PivotLayout.kt +++ b/dpadrecyclerview/src/main/java/com/rubensousa/dpadrecyclerview/layoutmanager/layout/PivotLayout.kt @@ -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?) {