Skip to content

Commit de57327

Browse files
Jiaqi Wufacebook-github-bot
authored andcommitted
Fix Horizontal ScrollView's scroll position during layout changes
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
1 parent 1b2a552 commit de57327

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
public class ReactHorizontalScrollContainerView extends ViewGroup {
1515

1616
private int mLayoutDirection;
17+
private int mCurrentWidth;
1718

1819
public ReactHorizontalScrollContainerView(Context context) {
1920
super(context);
2021
mLayoutDirection =
2122
I18nUtil.getInstance().isRTL(context) ? LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
23+
mCurrentWidth = 0;
2224
}
2325

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

35-
// Fix the ScrollX position when using RTL language
36-
int offsetX = computeHorizontalScrollRange() - getScrollX();
37-
3837
// Call with the present values in order to re-layout if necessary
3938
HorizontalScrollView parent = (HorizontalScrollView) getParent();
39+
// Fix the ScrollX position when using RTL language
40+
int offsetX = parent.getScrollX() + getWidth() - mCurrentWidth;
4041
parent.scrollTo(offsetX, parent.getScrollY());
4142
}
43+
mCurrentWidth = getWidth();
4244
}
4345
}

0 commit comments

Comments
 (0)