Skip to content

Commit 9f6f971

Browse files
genkikondofacebook-github-bot
authored andcommitted
Fix ReactHorizontalScrollView contentOffset
Summary: Brings the same fix https://www.internalfb.com/diff/D34015853 (be260b9) for ReactScrollView to ReactHorizontalScrollView When setting ScrollView's contentOffset, if the ScrollView hasn't been laid out yet when ReactHorizontalScrollViewManager.setContentOffset is called, then scroll position is never set properly. This is because the actual scroll offset (0, 0) was being passed into setPendingContentOffsets, instead of the desired scroll offset. Thus, when ReactHorizontalScrollView.onLayout gets called, ReactHorizontalScrollView.scrollTo gets called with (0, 0). Changelog: [Android][Fixed] - Fix ReactHorizontalScrollView contentOffset Reviewed By: bvanderhoof Differential Revision: D34246489 fbshipit-source-id: d923f7c9f136f7275d64bd658ffd5c2cc049d392
1 parent 90b98ef commit 9f6f971

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -661,8 +661,7 @@ private int getSnapInterval() {
661661
}
662662

663663
private View getContentView() {
664-
View contentView = getChildAt(0);
665-
return contentView;
664+
return getChildAt(0);
666665
}
667666

668667
public void setEndFillColor(int color) {
@@ -1191,12 +1190,13 @@ public void scrollTo(int x, int y) {
11911190
}
11921191

11931192
super.scrollTo(x, y);
1194-
// The final scroll position might be different from (x, y). For example, we may need to scroll
1195-
// to the last item in the list, but that item cannot be move to the start position of the view.
1196-
final int actualX = getScrollX();
1197-
final int actualY = getScrollY();
1198-
ReactScrollViewHelper.updateFabricScrollState(this, actualX, actualY);
1199-
setPendingContentOffsets(actualX, actualY);
1193+
ReactScrollViewHelper.updateFabricScrollState(this);
1194+
setPendingContentOffsets(x, y);
1195+
}
1196+
1197+
private boolean isContentReady() {
1198+
View child = getContentView();
1199+
return child != null && child.getWidth() != 0 && child.getHeight() != 0;
12001200
}
12011201

12021202
/**
@@ -1210,8 +1210,8 @@ private void setPendingContentOffsets(int x, int y) {
12101210
if (DEBUG_MODE) {
12111211
FLog.i(TAG, "setPendingContentOffsets[%d] x %d y %d", getId(), x, y);
12121212
}
1213-
View child = getContentView();
1214-
if (child != null && child.getWidth() != 0 && child.getHeight() != 0) {
1213+
1214+
if (isContentReady()) {
12151215
pendingContentOffsetX = UNSET_CONTENT_OFFSET;
12161216
pendingContentOffsetY = UNSET_CONTENT_OFFSET;
12171217
} else {

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -954,10 +954,13 @@ public void reactSmoothScrollTo(int x, int y) {
954954
}
955955

956956
/**
957-
* Calls `updateFabricScrollState` and updates state.
957+
* Calls `super.scrollTo` and updates state.
958958
*
959-
* <p>`scrollTo` changes `contentOffset` and we need to keep `contentOffset` in sync between
960-
* scroll view and state. Calling ScrollView's `scrollTo` doesn't update state.
959+
* <p>`super.scrollTo` changes `contentOffset` and we need to keep `contentOffset` in sync between
960+
* scroll view and state.
961+
*
962+
* <p>Note that while we can override scrollTo, we *cannot* override `smoothScrollTo` because it
963+
* is final. See `reactSmoothScrollTo`.
961964
*/
962965
@Override
963966
public void scrollTo(int x, int y) {
@@ -967,7 +970,7 @@ public void scrollTo(int x, int y) {
967970
}
968971

969972
private boolean isContentReady() {
970-
View child = getChildAt(0);
973+
View child = getContentView();
971974
return child != null && child.getWidth() != 0 && child.getHeight() != 0;
972975
}
973976

0 commit comments

Comments
 (0)