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: https://github.com/facebook/react-native/issues/19979)

Reviewed By: mdvacca

Differential Revision: D8772780

fbshipit-source-id: 969dcead550f4a3d24d06416b63d960492b7a124
This commit is contained in:
Jiaqi Wu 2018-07-19 17:00:46 -07:00 committed by Facebook Github Bot
parent 1b2a552955
commit de573277bf
1 changed files with 5 additions and 3 deletions

View File

@ -14,11 +14,13 @@ import com.facebook.react.modules.i18nmanager.I18nUtil;
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
@ -32,12 +34,12 @@ public class ReactHorizontalScrollContainerView extends ViewGroup {
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();
}
}