From ed29ba13f97f240c91fdf6c0ef3fb601046697b9 Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Thu, 23 Apr 2020 15:36:20 -0700 Subject: [PATCH] Support `contentOffset` property in Android's ScrollView and HorizontalScrollView Summary: For a very long time, iOS has supported the `contentOffset` property but Android has not: https://github.com/facebook/react-native/issues/6849 This property can be used, primarily, to autoscroll the ScrollView to a starting position when it is first rendered, to avoid "jumps" that occur by asynchronously scrolling to a start position. Changelog: [Android][Changed] ScrollView now supports `contentOffset` Reviewed By: mdvacca Differential Revision: D21198236 fbshipit-source-id: 2b0773569ba42120cb1fcf0f3847ca98af2285e7 --- ...roidHorizontalScrollViewNativeComponent.js | 1 + .../scroll/ReactHorizontalScrollView.java | 32 ++++++++++++++++++- .../ReactHorizontalScrollViewManager.java | 8 +++++ .../react/views/scroll/ReactScrollView.java | 32 ++++++++++++++++++- 4 files changed, 71 insertions(+), 2 deletions(-) diff --git a/Libraries/Components/ScrollView/AndroidHorizontalScrollViewNativeComponent.js b/Libraries/Components/ScrollView/AndroidHorizontalScrollViewNativeComponent.js index 22da77e56f1..2fed508a35a 100644 --- a/Libraries/Components/ScrollView/AndroidHorizontalScrollViewNativeComponent.js +++ b/Libraries/Components/ScrollView/AndroidHorizontalScrollViewNativeComponent.js @@ -37,6 +37,7 @@ const AndroidHorizontalScrollViewViewConfig = { snapToInterval: true, snapToStart: true, snapToOffsets: true, + contentOffset: true, }, }; diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java index 0dee6f7c8e9..c905c5dc9a4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java @@ -50,6 +50,8 @@ public class ReactHorizontalScrollView extends HorizontalScrollView private static final String CONTENT_OFFSET_LEFT = "contentOffsetLeft"; private static final String CONTENT_OFFSET_TOP = "contentOffsetTop"; + private static final int UNSET_CONTENT_OFFSET = -1; + private final OnScrollDispatchHelper mOnScrollDispatchHelper = new OnScrollDispatchHelper(); private final @Nullable OverScroller mScroller; private final VelocityHelper mVelocityHelper = new VelocityHelper(); @@ -76,6 +78,8 @@ public class ReactHorizontalScrollView extends HorizontalScrollView private boolean mSnapToEnd = true; private ReactViewBackgroundManager mReactBackgroundManager; private boolean mPagedArrowScrolling = false; + private int pendingContentOffsetX = UNSET_CONTENT_OFFSET; + private int pendingContentOffsetY = UNSET_CONTENT_OFFSET; private @Nullable StateWrapper mStateWrapper; private final Rect mTempRect = new Rect(); @@ -224,7 +228,13 @@ public class ReactHorizontalScrollView extends HorizontalScrollView @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // Call with the present values in order to re-layout if necessary - reactScrollTo(getScrollX(), getScrollY()); + // If a "pending" value has been set, we restore that value. + // That value gets cleared by reactScrollTo. + int scrollToX = + pendingContentOffsetX != UNSET_CONTENT_OFFSET ? pendingContentOffsetX : getScrollX(); + int scrollToY = + pendingContentOffsetY != UNSET_CONTENT_OFFSET ? pendingContentOffsetY : getScrollY(); + reactScrollTo(scrollToX, scrollToY); } /** @@ -906,6 +916,7 @@ public class ReactHorizontalScrollView extends HorizontalScrollView public void reactSmoothScrollTo(int x, int y) { smoothScrollTo(x, y); updateStateOnScroll(x, y); + setPendingContentOffsets(x, y); } /** @@ -917,6 +928,25 @@ public class ReactHorizontalScrollView extends HorizontalScrollView public void reactScrollTo(int x, int y) { scrollTo(x, y); updateStateOnScroll(x, y); + setPendingContentOffsets(x, y); + } + + /** + * If contentOffset is set before the View has been laid out, store the values and set them when + * `onLayout` is called. + * + * @param x + * @param y + */ + private void setPendingContentOffsets(int x, int y) { + View child = getChildAt(0); + if (child != null && child.getWidth() != 0 && child.getHeight() != 0) { + pendingContentOffsetX = UNSET_CONTENT_OFFSET; + pendingContentOffsetY = UNSET_CONTENT_OFFSET; + } else { + pendingContentOffsetX = x; + pendingContentOffsetY = y; + } } public void updateState(@Nullable StateWrapper stateWrapper) { diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java index b7712b3d653..8c2b91637ae 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java @@ -12,6 +12,7 @@ import android.util.DisplayMetrics; import androidx.annotation.Nullable; import androidx.core.view.ViewCompat; import com.facebook.react.bridge.ReadableArray; +import com.facebook.react.bridge.ReadableMap; import com.facebook.react.module.annotations.ReactModule; import com.facebook.react.uimanager.DisplayMetricsHolder; import com.facebook.react.uimanager.PixelUtil; @@ -299,4 +300,11 @@ public class ReactHorizontalScrollViewManager extends ViewGroupManager