|
33 | 33 | import com.facebook.common.logging.FLog;
|
34 | 34 | import com.facebook.infer.annotation.Assertions;
|
35 | 35 | import com.facebook.react.R;
|
| 36 | +import com.facebook.react.bridge.ReadableMap; |
36 | 37 | import com.facebook.react.common.ReactConstants;
|
37 | 38 | import com.facebook.react.uimanager.FabricViewStateManager;
|
38 | 39 | import com.facebook.react.uimanager.MeasureSpecAssertions;
|
| 40 | +import com.facebook.react.uimanager.PixelUtil; |
39 | 41 | import com.facebook.react.uimanager.PointerEvents;
|
40 | 42 | import com.facebook.react.uimanager.ReactClippingViewGroup;
|
41 | 43 | import com.facebook.react.uimanager.ReactClippingViewGroupHelper;
|
@@ -100,6 +102,7 @@ public class ReactScrollView extends ScrollView
|
100 | 102 | private int mSnapToAlignment = SNAP_ALIGNMENT_DISABLED;
|
101 | 103 | private @Nullable View mContentView;
|
102 | 104 | private ReactViewBackgroundManager mReactBackgroundManager;
|
| 105 | + private @Nullable ReadableMap mCurrentContentOffset = null; |
103 | 106 | private int pendingContentOffsetX = UNSET_CONTENT_OFFSET;
|
104 | 107 | private int pendingContentOffsetY = UNSET_CONTENT_OFFSET;
|
105 | 108 | private final FabricViewStateManager mFabricViewStateManager = new FabricViewStateManager();
|
@@ -1002,6 +1005,23 @@ public void onChildViewRemoved(View parent, View child) {
|
1002 | 1005 | mContentView = null;
|
1003 | 1006 | }
|
1004 | 1007 |
|
| 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 | + |
1005 | 1025 | /**
|
1006 | 1026 | * Calls `smoothScrollTo` and updates state.
|
1007 | 1027 | *
|
|
0 commit comments