Skip to content

Commit

Permalink
Fix Horizontal ScrollView's scroll position during layout changes
Browse files Browse the repository at this point in the history
Summary: Fix the calculation of offsetX in onLayout (ReactHorizontalScrollContainerView.java) that re-positions the updated layout. A private instance variable (oldWidth) is added in order to track the width difference between consecutive updates. (Issue report: #19979)

Reviewed By: mdvacca

Differential Revision: D8772780

fbshipit-source-id: 969dcead550f4a3d24d06416b63d960492b7a124
  • Loading branch information
Jiaqi Wu authored and facebook-github-bot committed Jul 20, 2018
1 parent 1b2a552 commit de57327
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
public class ReactHorizontalScrollContainerView extends ViewGroup {

private int mLayoutDirection;
private int mCurrentWidth;

public ReactHorizontalScrollContainerView(Context context) {
super(context);
mLayoutDirection =
I18nUtil.getInstance().isRTL(context) ? LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
mCurrentWidth = 0;
}

@Override
Expand All @@ -32,12 +34,12 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
setLeft(newLeft);
setRight(newRight);

// Fix the ScrollX position when using RTL language
int offsetX = computeHorizontalScrollRange() - getScrollX();

// Call with the present values in order to re-layout if necessary
HorizontalScrollView parent = (HorizontalScrollView) getParent();
// Fix the ScrollX position when using RTL language
int offsetX = parent.getScrollX() + getWidth() - mCurrentWidth;
parent.scrollTo(offsetX, parent.getScrollY());
}
mCurrentWidth = getWidth();
}
}

0 comments on commit de57327

Please sign in to comment.