Skip to content

Commit 0eeb93b

Browse files
committed
Do not track Android ScrollView scroll velocity if scroll changed outside gestures
1 parent 85f1d9a commit 0eeb93b

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/OnScrollDispatchHelper.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,21 @@ public class OnScrollDispatchHelper {
3030
* Call from a ScrollView in onScrollChanged, returns true if this onScrollChanged is legit (not a
3131
* duplicate) and should be dispatched.
3232
*/
33-
public fun onScrollChanged(x: Int, y: Int): Boolean {
33+
public fun onScrollChanged(x: Int, y: Int, duringGesture: Boolean): Boolean {
3434
val eventTime = SystemClock.uptimeMillis()
3535
val shouldDispatch =
3636
eventTime - lastScrollEventTimeMs > MIN_EVENT_SEPARATION_MS || prevX != x || prevY != y
37-
if (eventTime - lastScrollEventTimeMs != 0L) {
38-
xFlingVelocity = (x - prevX).toFloat() / (eventTime - lastScrollEventTimeMs)
39-
yFlingVelocity = (y - prevY).toFloat() / (eventTime - lastScrollEventTimeMs)
37+
if (duringGesture) {
38+
if (eventTime - lastScrollEventTimeMs != 0L) {
39+
xFlingVelocity = (x - prevX).toFloat() / (eventTime - lastScrollEventTimeMs)
40+
yFlingVelocity = (y - prevY).toFloat() / (eventTime - lastScrollEventTimeMs)
41+
}
42+
} else {
43+
// Reset velocity if scroll change was not caused by a gesture, but e.g.:
44+
// - manual scrollTo() call
45+
// - internal scrollTo() call (e.g. in rtl, when list resizes)
46+
xFlingVelocity = 0f
47+
yFlingVelocity = 0f
4048
}
4149
lastScrollEventTimeMs = eventTime
4250
prevX = x

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ protected void onScrollChanged(int x, int y, int oldX, int oldY) {
488488

489489
mActivelyScrolling = true;
490490

491-
if (mOnScrollDispatchHelper.onScrollChanged(x, y)) {
491+
if (mOnScrollDispatchHelper.onScrollChanged(x, y, mDragging)) {
492492
if (mRemoveClippedSubviews) {
493493
updateClippingRect();
494494
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ protected void onScrollChanged(int x, int y, int oldX, int oldY) {
414414

415415
mActivelyScrolling = true;
416416

417-
if (mOnScrollDispatchHelper.onScrollChanged(x, y)) {
417+
if (mOnScrollDispatchHelper.onScrollChanged(x, y, mDragging)) {
418418
if (mRemoveClippedSubviews) {
419419
updateClippingRect();
420420
}

0 commit comments

Comments
 (0)