From 6c19996e10eda97a3501f53a68da246d1f122d01 Mon Sep 17 00:00:00 2001 From: Fabrizio Cucci Date: Mon, 14 Oct 2024 04:28:31 -0700 Subject: [PATCH] Account for diffrent frame size when using maintainVisibleContentPosition in virtualized lists (#46979) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46979 It seems that, when the items in a virtualized list don't have a fixed size (i.e. they dynamically scale with the size of the container), we need to also take into account their size when updating the scroll position as a consequense of setting `maintainVisibleContentPosition`. Changelog: [Android][Fixed] - Account for items dynamically scaling with the container when using `maintainVisibleContentPosition` in virtualized lists Reviewed By: javache, NickGerleman Differential Revision: D64238887 fbshipit-source-id: 9f05a461d178bc191137b1a350072337ba62e224 --- .../views/scroll/MaintainVisibleScrollPositionHelper.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/MaintainVisibleScrollPositionHelper.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/MaintainVisibleScrollPositionHelper.java index 26c9aebc8388ad..bfa9da9b0c1af4 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/MaintainVisibleScrollPositionHelper.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/MaintainVisibleScrollPositionHelper.java @@ -115,7 +115,9 @@ private void updateScrollPositionInternal() { firstVisibleView.getHitRect(newFrame); if (mHorizontal) { - int deltaX = newFrame.left - mPrevFirstVisibleFrame.left; + int deltaX = + (newFrame.left - mPrevFirstVisibleFrame.left) + + (newFrame.width() - mPrevFirstVisibleFrame.width()); if (deltaX != 0) { int scrollX = mScrollView.getScrollX(); mScrollView.scrollToPreservingMomentum(scrollX + deltaX, mScrollView.getScrollY()); @@ -126,7 +128,9 @@ private void updateScrollPositionInternal() { } } } else { - int deltaY = newFrame.top - mPrevFirstVisibleFrame.top; + int deltaY = + (newFrame.top - mPrevFirstVisibleFrame.top) + + (newFrame.height() - mPrevFirstVisibleFrame.height()); if (deltaY != 0) { int scrollY = mScrollView.getScrollY(); mScrollView.scrollToPreservingMomentum(mScrollView.getScrollX(), scrollY + deltaY);