Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing RTL HorizontalScrolling in Android
Reviewed By: astreet Differential Revision: D6170631 fbshipit-source-id: 254e6ed9a4d6e42b6d1215de1ff63aedb2c07a0a
- Loading branch information
1 parent
e707891
commit c278020
Showing
5 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...oid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollContainerView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
package com.facebook.react.views.scroll; | ||
|
||
import android.content.Context; | ||
import android.view.ViewGroup; | ||
import android.widget.HorizontalScrollView; | ||
import com.facebook.react.modules.i18nmanager.I18nUtil; | ||
|
||
/** Container of Horizontal scrollViews that supports RTL scrolling. */ | ||
public class ReactHorizontalScrollContainerView extends ViewGroup { | ||
|
||
private int mLayoutDirection; | ||
|
||
public ReactHorizontalScrollContainerView(Context context) { | ||
super(context); | ||
mLayoutDirection = | ||
I18nUtil.getInstance().isRTL(context) ? LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR; | ||
} | ||
|
||
@Override | ||
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { | ||
if (mLayoutDirection == LAYOUT_DIRECTION_RTL) { | ||
// When the layout direction is RTL, we expect Yoga to give us a layout | ||
// that extends off the screen to the left so we re-center it with left=0 | ||
int newLeft = 0; | ||
int width = right - left; | ||
int newRight = newLeft + width; | ||
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(); | ||
parent.scrollTo(offsetX, parent.getScrollY()); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
.../main/java/com/facebook/react/views/scroll/ReactHorizontalScrollContainerViewManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
package com.facebook.react.views.scroll; | ||
|
||
import com.facebook.react.module.annotations.ReactModule; | ||
import com.facebook.react.uimanager.ThemedReactContext; | ||
import com.facebook.react.uimanager.ViewGroupManager; | ||
|
||
/** View manager for {@link ReactHorizontalScrollContainerView} components. */ | ||
@ReactModule(name = ReactHorizontalScrollContainerViewManager.REACT_CLASS) | ||
public class ReactHorizontalScrollContainerViewManager | ||
extends ViewGroupManager<ReactHorizontalScrollContainerView> { | ||
|
||
protected static final String REACT_CLASS = "AndroidHorizontalScrollContentView"; | ||
|
||
public ReactHorizontalScrollContainerViewManager() {} | ||
|
||
@Override | ||
public String getName() { | ||
return REACT_CLASS; | ||
} | ||
|
||
@Override | ||
public ReactHorizontalScrollContainerView createViewInstance(ThemedReactContext context) { | ||
return new ReactHorizontalScrollContainerView(context); | ||
} | ||
} |