Skip to content

Commit b12213d

Browse files
mdvaccagrabbou
authored andcommitted
Fixing RTL HorizontalScrolling in Android
Reviewed By: astreet Differential Revision: D6170631 fbshipit-source-id: 254e6ed9a4d6e42b6d1215de1ff63aedb2c07a0a
1 parent 2da00fe commit b12213d

5 files changed

Lines changed: 76 additions & 1 deletion

File tree

Libraries/Components/ScrollView/ScrollView.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,10 +659,11 @@ const ScrollView = createReactClass({
659659
} else if (Platform.OS === 'android') {
660660
if (this.props.horizontal) {
661661
ScrollViewClass = AndroidHorizontalScrollView;
662+
ScrollContentContainerViewClass = AndroidHorizontalScrollContentView;
662663
} else {
663664
ScrollViewClass = AndroidScrollView;
665+
ScrollContentContainerViewClass = View;
664666
}
665-
ScrollContentContainerViewClass = View;
666667
}
667668

668669
invariant(
@@ -879,6 +880,7 @@ const styles = StyleSheet.create({
879880

880881
let nativeOnlyProps,
881882
AndroidScrollView,
883+
AndroidHorizontalScrollContentView,
882884
AndroidHorizontalScrollView,
883885
RCTScrollView,
884886
RCTScrollContentView;
@@ -898,6 +900,9 @@ if (Platform.OS === 'android') {
898900
(ScrollView: React.ComponentType<any>),
899901
nativeOnlyProps
900902
);
903+
AndroidHorizontalScrollContentView = requireNativeComponent(
904+
'AndroidHorizontalScrollContentView'
905+
);
901906
} else if (Platform.OS === 'ios') {
902907
nativeOnlyProps = {
903908
nativeOnly: {

ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import com.facebook.react.views.picker.ReactDialogPickerManager;
6262
import com.facebook.react.views.picker.ReactDropdownPickerManager;
6363
import com.facebook.react.views.progressbar.ReactProgressBarViewManager;
64+
import com.facebook.react.views.scroll.ReactHorizontalScrollContainerViewManager;
6465
import com.facebook.react.views.scroll.ReactHorizontalScrollViewManager;
6566
import com.facebook.react.views.scroll.ReactScrollViewManager;
6667
import com.facebook.react.views.slider.ReactSliderManager;
@@ -315,6 +316,7 @@ public List<ViewManager> createViewManagers(ReactApplicationContext reactContext
315316
viewManagers.add(new ReactDrawerLayoutManager());
316317
viewManagers.add(new ReactDropdownPickerManager());
317318
viewManagers.add(new ReactHorizontalScrollViewManager());
319+
viewManagers.add(new ReactHorizontalScrollContainerViewManager());
318320
viewManagers.add(new ReactProgressBarViewManager());
319321
viewManagers.add(new ReactScrollViewManager());
320322
viewManagers.add(new ReactSliderManager());

ReactAndroid/src/main/java/com/facebook/react/views/scroll/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ android_library(
1616
react_native_target("java/com/facebook/react/bridge:bridge"),
1717
react_native_target("java/com/facebook/react/common:common"),
1818
react_native_target("java/com/facebook/react/module/annotations:annotations"),
19+
react_native_target("java/com/facebook/react/modules/i18nmanager:i18nmanager"),
1920
react_native_target("java/com/facebook/react/touch:touch"),
2021
react_native_target("java/com/facebook/react/uimanager:uimanager"),
2122
react_native_target("java/com/facebook/react/uimanager/annotations:annotations"),
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2004-present Facebook. All Rights Reserved.
2+
3+
package com.facebook.react.views.scroll;
4+
5+
import android.content.Context;
6+
import android.view.ViewGroup;
7+
import android.widget.HorizontalScrollView;
8+
import com.facebook.react.modules.i18nmanager.I18nUtil;
9+
10+
/** Container of Horizontal scrollViews that supports RTL scrolling. */
11+
public class ReactHorizontalScrollContainerView extends ViewGroup {
12+
13+
private int mLayoutDirection;
14+
15+
public ReactHorizontalScrollContainerView(Context context) {
16+
super(context);
17+
mLayoutDirection =
18+
I18nUtil.getInstance().isRTL(context) ? LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
19+
}
20+
21+
@Override
22+
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
23+
if (mLayoutDirection == LAYOUT_DIRECTION_RTL) {
24+
// When the layout direction is RTL, we expect Yoga to give us a layout
25+
// that extends off the screen to the left so we re-center it with left=0
26+
int newLeft = 0;
27+
int width = right - left;
28+
int newRight = newLeft + width;
29+
setLeft(newLeft);
30+
setRight(newRight);
31+
32+
// Fix the ScrollX position when using RTL language
33+
int offsetX = computeHorizontalScrollRange() - getScrollX();
34+
35+
// Call with the present values in order to re-layout if necessary
36+
HorizontalScrollView parent = (HorizontalScrollView) getParent();
37+
parent.scrollTo(offsetX, parent.getScrollY());
38+
}
39+
}
40+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2004-present Facebook. All Rights Reserved.
2+
3+
package com.facebook.react.views.scroll;
4+
5+
import com.facebook.react.module.annotations.ReactModule;
6+
import com.facebook.react.uimanager.ThemedReactContext;
7+
import com.facebook.react.uimanager.ViewGroupManager;
8+
9+
/** View manager for {@link ReactHorizontalScrollContainerView} components. */
10+
@ReactModule(name = ReactHorizontalScrollContainerViewManager.REACT_CLASS)
11+
public class ReactHorizontalScrollContainerViewManager
12+
extends ViewGroupManager<ReactHorizontalScrollContainerView> {
13+
14+
protected static final String REACT_CLASS = "AndroidHorizontalScrollContentView";
15+
16+
public ReactHorizontalScrollContainerViewManager() {}
17+
18+
@Override
19+
public String getName() {
20+
return REACT_CLASS;
21+
}
22+
23+
@Override
24+
public ReactHorizontalScrollContainerView createViewInstance(ThemedReactContext context) {
25+
return new ReactHorizontalScrollContainerView(context);
26+
}
27+
}

0 commit comments

Comments
 (0)