Skip to content

Commit 32cb9ec

Browse files
dmainasfacebook-github-bot
authored andcommitted
Fix Inverted Horizontal ScrollView on Android (#23233)
Summary: ScrollView (as well as FlatList) used with horizontal={true} and inverted={true} do not scroll as expected due to a known Android P bug. Fixes #22710. This is pretty much a clone of PR #21117, applied to a horizontal ScrollView. [Android] [Fixed] - Fix Inverted Horizontal ScrollView on Android 1. Create a test application with a FlatList with horizontal={true} inverted={true}. 2. Fill it with data 3. Scroll and release your finger Pull Request resolved: #23233 Differential Revision: D13915911 Pulled By: cpojer fbshipit-source-id: d32c82e6d9076f5ffdf342fcd71bd921e9c8a97b
1 parent e4364fa commit 32cb9ec

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

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

+14-4
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,18 @@ public boolean onTouchEvent(MotionEvent ev) {
286286

287287
@Override
288288
public void fling(int velocityX) {
289+
290+
// Workaround.
291+
// On Android P if a ScrollView is inverted, we will get a wrong sign for
292+
// velocityX (see https://issuetracker.google.com/issues/112385925).
293+
// At the same time, mOnScrollDispatchHelper tracks the correct velocity direction.
294+
//
295+
// Hence, we can use the absolute value from whatever the OS gives
296+
// us and use the sign of what mOnScrollDispatchHelper has tracked.
297+
final int correctedVelocityX = (int)(Math.abs(velocityX) * Math.signum(mOnScrollDispatchHelper.getXFlingVelocity()));
298+
289299
if (mPagingEnabled) {
290-
flingAndSnap(velocityX);
300+
flingAndSnap(correctedVelocityX);
291301
} else if (mScroller != null) {
292302
// FB SCROLLVIEW CHANGE
293303

@@ -302,7 +312,7 @@ public void fling(int velocityX) {
302312
mScroller.fling(
303313
getScrollX(), // startX
304314
getScrollY(), // startY
305-
velocityX, // velocityX
315+
correctedVelocityX, // velocityX
306316
0, // velocityY
307317
0, // minX
308318
Integer.MAX_VALUE, // maxX
@@ -316,9 +326,9 @@ public void fling(int velocityX) {
316326

317327
// END FB SCROLLVIEW CHANGE
318328
} else {
319-
super.fling(velocityX);
329+
super.fling(correctedVelocityX);
320330
}
321-
handlePostTouchScrolling(velocityX, 0);
331+
handlePostTouchScrolling(correctedVelocityX, 0);
322332
}
323333

324334
@Override

0 commit comments

Comments
 (0)