Skip to content

Commit

Permalink
Fix ScrollView contentOffset
Browse files Browse the repository at this point in the history
Summary:
When setting ScrollView's contentOffset, if the ScrollView hasn't been laid out yet when ReactScrollViewManager.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 ReactScrollView.onLayout gets called, ReactScrollView.scrollTo gets called with (0, 0).

Also updates out of date comments,

Changelog:
[Android][Fixed] - Fix ScrollView contentOffset

Reviewed By: ryancat

Differential Revision: D34015853

fbshipit-source-id: 84141a663fdb0ace2be7cef61f14944cb08125d1
  • Loading branch information
genkikondo authored and facebook-github-bot committed Feb 5, 2022
1 parent b8a2f34 commit be260b9
Showing 1 changed file with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
@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
// If a "pending" value has been set, we restore that value.
// That value gets cleared by reactScrollTo.
// If a "pending" content offset value has been set, we restore that value.
// Upon call to scrollTo, the "pending" values will be re-set.
int scrollToX =
pendingContentOffsetX != UNSET_CONTENT_OFFSET ? pendingContentOffsetX : getScrollX();
int scrollToY =
Expand Down Expand Up @@ -954,23 +954,21 @@ public void reactSmoothScrollTo(int x, int y) {
}

/**
* Calls `reactScrollTo` and updates state.
* Calls `updateFabricScrollState` and updates state.
*
* <p>`reactScrollTo` changes `contentOffset` and we need to keep `contentOffset` in sync between
* scroll view and state. Calling raw `reactScrollTo` doesn't update state.
*
* <p>Note that while we can override scrollTo, we *cannot* override `smoothScrollTo` because it
* is final. See `reactSmoothScrollTo`.
* <p>`scrollTo` changes `contentOffset` and we need to keep `contentOffset` in sync between
* scroll view and state. Calling ScrollView's `scrollTo` doesn't update state.
*/
@Override
public void scrollTo(int x, int y) {
super.scrollTo(x, y);
// The final scroll position might be different from (x, y). For example, we may need to scroll
// to the last item in the list, but that item cannot be move to the start position of the view.
final int actualX = getScrollX();
final int actualY = getScrollY();
ReactScrollViewHelper.updateFabricScrollState(this, actualX, actualY);
setPendingContentOffsets(actualX, actualY);
ReactScrollViewHelper.updateFabricScrollState(this);
setPendingContentOffsets(x, y);
}

private boolean isContentReady() {
View child = getChildAt(0);
return child != null && child.getWidth() != 0 && child.getHeight() != 0;
}

/**
Expand All @@ -981,8 +979,7 @@ public void scrollTo(int x, int y) {
* @param y
*/
private void setPendingContentOffsets(int x, int y) {
View child = getChildAt(0);
if (child != null && child.getWidth() != 0 && child.getHeight() != 0) {
if (isContentReady()) {
pendingContentOffsetX = UNSET_CONTENT_OFFSET;
pendingContentOffsetY = UNSET_CONTENT_OFFSET;
} else {
Expand Down

0 comments on commit be260b9

Please sign in to comment.