Skip to content

Commit 14330b5

Browse files
skinssharkfacebook-github-bot
authored andcommitted
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
1 parent 09ad0cc commit 14330b5

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
import com.facebook.common.logging.FLog;
3434
import com.facebook.infer.annotation.Assertions;
3535
import com.facebook.react.R;
36+
import com.facebook.react.bridge.ReadableMap;
3637
import com.facebook.react.common.ReactConstants;
3738
import com.facebook.react.uimanager.FabricViewStateManager;
3839
import com.facebook.react.uimanager.MeasureSpecAssertions;
40+
import com.facebook.react.uimanager.PixelUtil;
3941
import com.facebook.react.uimanager.PointerEvents;
4042
import com.facebook.react.uimanager.ReactClippingViewGroup;
4143
import com.facebook.react.uimanager.ReactClippingViewGroupHelper;
@@ -100,6 +102,7 @@ public class ReactScrollView extends ScrollView
100102
private int mSnapToAlignment = SNAP_ALIGNMENT_DISABLED;
101103
private @Nullable View mContentView;
102104
private ReactViewBackgroundManager mReactBackgroundManager;
105+
private @Nullable ReadableMap mCurrentContentOffset = null;
103106
private int pendingContentOffsetX = UNSET_CONTENT_OFFSET;
104107
private int pendingContentOffsetY = UNSET_CONTENT_OFFSET;
105108
private final FabricViewStateManager mFabricViewStateManager = new FabricViewStateManager();
@@ -1002,6 +1005,23 @@ public void onChildViewRemoved(View parent, View child) {
10021005
mContentView = null;
10031006
}
10041007

1008+
public void setContentOffset(ReadableMap value) {
1009+
// When contentOffset={{x:0,y:0}} with lazy load items, setContentOffset
1010+
// is repeatedly called, causing an unexpected scroll to top behavior.
1011+
// Avoid updating contentOffset if the value has not changed.
1012+
if (mCurrentContentOffset == null || !mCurrentContentOffset.equals(value)) {
1013+
mCurrentContentOffset = value;
1014+
1015+
if (value != null) {
1016+
double x = value.hasKey("x") ? value.getDouble("x") : 0;
1017+
double y = value.hasKey("y") ? value.getDouble("y") : 0;
1018+
scrollTo((int) PixelUtil.toPixelFromDIP(x), (int) PixelUtil.toPixelFromDIP(y));
1019+
} else {
1020+
scrollTo(0, 0);
1021+
}
1022+
}
1023+
}
1024+
10051025
/**
10061026
* Calls `smoothScrollTo` and updates state.
10071027
*

ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,7 @@ public void setFadingEdgeLength(ReactScrollView view, int value) {
320320

321321
@ReactProp(name = "contentOffset", customType = "Point")
322322
public void setContentOffset(ReactScrollView view, ReadableMap value) {
323-
if (value != null) {
324-
double x = value.hasKey("x") ? value.getDouble("x") : 0;
325-
double y = value.hasKey("y") ? value.getDouble("y") : 0;
326-
view.scrollTo((int) PixelUtil.toPixelFromDIP(x), (int) PixelUtil.toPixelFromDIP(y));
327-
} else {
328-
view.scrollTo(0, 0);
329-
}
323+
view.setContentOffset(value);
330324
}
331325

332326
@Override

0 commit comments

Comments
 (0)