Skip to content

Commit c7c263d

Browse files
dhleongfacebook-github-bot
authored andcommitted
Fix unexpected ScrollView fling behavior due to Android P bug workaround (#34233)
Summary: Some custom logic is applied to workaround a platform bug where velocity may be incorrect on Android P. [The bug in question](https://issuetracker.google.com/issues/112385925) appears to have been fixed before Android `Q` was released, so we shouldn't *need* to apply the workaround on other versions. As described in #34226 the workaround can adversely affect certain scroll behaviors, which can easily be reproduced when you briefly scroll one direction then quickly fling the opposite direction (see the video in the linked ticket). This PR changes the workaround to *only* be applied on Android P, in order to avoid causing weird scroll behavior on versions that are not actually affected by the bug the workaround is working around. ## Changelog ``` [Android] [Fixed] - Fix occasionally incorrect ScrollView fling behavior ``` Pull Request resolved: #34233 Test Plan: - Repro the strange fling behavior in the current version (See video attached in #34226) - Verify that the string fling behavior is fixed with this patch - Verify that fling behavior still works as expected on Android versions affected by the [original bug](https://issuetracker.google.com/issues/112385925), and those immediately following it (to verify that the bug being worked around was, in fact, fixed as expected). Reviewed By: javache Differential Revision: D38287277 Pulled By: ryancat fbshipit-source-id: 2c786872c4d41655b3849bb92e02f1f16c663b41
1 parent 1bba590 commit c7c263d

1 file changed

Lines changed: 21 additions & 12 deletions

File tree

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.graphics.Rect;
2121
import android.graphics.drawable.ColorDrawable;
2222
import android.graphics.drawable.Drawable;
23+
import android.os.Build;
2324
import android.view.KeyEvent;
2425
import android.view.MotionEvent;
2526
import android.view.View;
@@ -480,18 +481,7 @@ public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point of
480481

481482
@Override
482483
public void fling(int velocityY) {
483-
// Workaround.
484-
// On Android P if a ScrollView is inverted, we will get a wrong sign for
485-
// velocityY (see https://issuetracker.google.com/issues/112385925).
486-
// At the same time, mOnScrollDispatchHelper tracks the correct velocity direction.
487-
//
488-
// Hence, we can use the absolute value from whatever the OS gives
489-
// us and use the sign of what mOnScrollDispatchHelper has tracked.
490-
float signum = Math.signum(mOnScrollDispatchHelper.getYFlingVelocity());
491-
if (signum == 0) {
492-
signum = Math.signum(velocityY);
493-
}
494-
final int correctedVelocityY = (int) (Math.abs(velocityY) * signum);
484+
final int correctedVelocityY = correctFlingVelocityY(velocityY);
495485

496486
if (mPagingEnabled) {
497487
flingAndSnap(correctedVelocityY);
@@ -530,6 +520,25 @@ public void fling(int velocityY) {
530520
handlePostTouchScrolling(0, correctedVelocityY);
531521
}
532522

523+
private int correctFlingVelocityY(int velocityY) {
524+
if (Build.VERSION.SDK_INT != Build.VERSION_CODES.P) {
525+
return velocityY;
526+
}
527+
528+
// Workaround.
529+
// On Android P if a ScrollView is inverted, we will get a wrong sign for
530+
// velocityY (see https://issuetracker.google.com/issues/112385925).
531+
// At the same time, mOnScrollDispatchHelper tracks the correct velocity direction.
532+
//
533+
// Hence, we can use the absolute value from whatever the OS gives
534+
// us and use the sign of what mOnScrollDispatchHelper has tracked.
535+
float signum = Math.signum(mOnScrollDispatchHelper.getYFlingVelocity());
536+
if (signum == 0) {
537+
signum = Math.signum(velocityY);
538+
}
539+
return (int) (Math.abs(velocityY) * signum);
540+
}
541+
533542
private void enableFpsListener() {
534543
if (isScrollPerfLoggingEnabled()) {
535544
Assertions.assertNotNull(mFpsListener);

0 commit comments

Comments
 (0)