From 14330b56c22a760a4f909d72fb4ebec2d8c78dad Mon Sep 17 00:00:00 2001 From: Sharon Zheng Date: Tue, 3 Jan 2023 11:23:44 -0800 Subject: [PATCH] track previous contentOffset to avoid duplicate calls Summary: - there was a bug when `contentOffset` was set to {x:0, y:0} and there were items lazy loaded in with prop changes- the View would consistently scroll back to the top after loading in new items - to avoid this, we store the offset value when it's first set, and only run the `scrollTo` logic then Changelog: [Internal][Fixed] - Fix contentOffset forcing scroll to top behavior Reviewed By: fkgozali, mdvacca Differential Revision: D42089871 fbshipit-source-id: 3968d98341728a45bec28e8783c9977e91dd4d2c --- .../react/views/scroll/ReactScrollView.java | 20 +++++++++++++++++++ .../views/scroll/ReactScrollViewManager.java | 8 +------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java index ae0a51274e7..16250d423d2 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java @@ -33,9 +33,11 @@ import androidx.core.view.ViewCompat; import com.facebook.common.logging.FLog; import com.facebook.infer.annotation.Assertions; import com.facebook.react.R; +import com.facebook.react.bridge.ReadableMap; import com.facebook.react.common.ReactConstants; import com.facebook.react.uimanager.FabricViewStateManager; import com.facebook.react.uimanager.MeasureSpecAssertions; +import com.facebook.react.uimanager.PixelUtil; import com.facebook.react.uimanager.PointerEvents; import com.facebook.react.uimanager.ReactClippingViewGroup; import com.facebook.react.uimanager.ReactClippingViewGroupHelper; @@ -100,6 +102,7 @@ public class ReactScrollView extends ScrollView private int mSnapToAlignment = SNAP_ALIGNMENT_DISABLED; private @Nullable View mContentView; private ReactViewBackgroundManager mReactBackgroundManager; + private @Nullable ReadableMap mCurrentContentOffset = null; private int pendingContentOffsetX = UNSET_CONTENT_OFFSET; private int pendingContentOffsetY = UNSET_CONTENT_OFFSET; private final FabricViewStateManager mFabricViewStateManager = new FabricViewStateManager(); @@ -1002,6 +1005,23 @@ public class ReactScrollView extends ScrollView mContentView = null; } + public void setContentOffset(ReadableMap value) { + // When contentOffset={{x:0,y:0}} with lazy load items, setContentOffset + // is repeatedly called, causing an unexpected scroll to top behavior. + // Avoid updating contentOffset if the value has not changed. + if (mCurrentContentOffset == null || !mCurrentContentOffset.equals(value)) { + mCurrentContentOffset = value; + + if (value != null) { + double x = value.hasKey("x") ? value.getDouble("x") : 0; + double y = value.hasKey("y") ? value.getDouble("y") : 0; + scrollTo((int) PixelUtil.toPixelFromDIP(x), (int) PixelUtil.toPixelFromDIP(y)); + } else { + scrollTo(0, 0); + } + } + } + /** * Calls `smoothScrollTo` and updates state. * diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java index 933455e8056..1a6ada453c7 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java @@ -320,13 +320,7 @@ public class ReactScrollViewManager extends ViewGroupManager @ReactProp(name = "contentOffset", customType = "Point") public void setContentOffset(ReactScrollView view, ReadableMap value) { - if (value != null) { - double x = value.hasKey("x") ? value.getDouble("x") : 0; - double y = value.hasKey("y") ? value.getDouble("y") : 0; - view.scrollTo((int) PixelUtil.toPixelFromDIP(x), (int) PixelUtil.toPixelFromDIP(y)); - } else { - view.scrollTo(0, 0); - } + view.setContentOffset(value); } @Override