From 0368081858193d7c2537acd9080d11bb701ee98b Mon Sep 17 00:00:00 2001 From: Xin Chen Date: Fri, 4 Mar 2022 15:50:00 -0800 Subject: [PATCH] Cancel the post touch fling when new touch is registered Summary: This diff aims to fix an issue I found while working on T113264056. It's to address the [comment](https://www.internalfb.com/diff/D3207608 (https://github.com/facebook/react-native/commit/a3146e41a259175f0c79e3c213523a0fb21613d7)?dst_version_fbid=507451319451602&transaction_fbid=1615731455398227) in an old diff D3207608 (https://github.com/facebook/react-native/commit/a3146e41a259175f0c79e3c213523a0fb21613d7). That issue begins to surface after I made the change in the next diff, which is discussed in detail in the task description (T113385381). The fix here is to cancel the post touch processing when a new touch down is received. This should've cancel any previous post touch processing as the new touch should take full control of scrolling. Changelog: [Android][Fixed] - Cancel post touch process when new touch is received Reviewed By: javache Differential Revision: D34627330 fbshipit-source-id: 1fb8cfc1a4d94349b5290915a0a9f190186f2af3 --- .../views/scroll/ReactHorizontalScrollView.java | 12 +++++++++++- .../facebook/react/views/scroll/ReactScrollView.java | 12 +++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java index 879acf55a78..1317c6e43de 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java @@ -531,7 +531,7 @@ public class ReactHorizontalScrollView extends HorizontalScrollView } mVelocityHelper.calculateVelocity(ev); - int action = ev.getAction() & MotionEvent.ACTION_MASK; + int action = ev.getActionMasked(); if (action == MotionEvent.ACTION_UP && mDragging) { ReactScrollViewHelper.updateFabricScrollState(this); @@ -544,6 +544,10 @@ public class ReactHorizontalScrollView extends HorizontalScrollView handlePostTouchScrolling(Math.round(velocityX), Math.round(velocityY)); } + if (action == MotionEvent.ACTION_DOWN) { + cancelPostTouchScrolling(); + } + return super.onTouchEvent(ev); } @@ -821,6 +825,12 @@ public class ReactHorizontalScrollView extends HorizontalScrollView this, mPostTouchRunnable, ReactScrollViewHelper.MOMENTUM_DELAY); } + private void cancelPostTouchScrolling() { + if (mPostTouchRunnable != null) { + removeCallbacks(mPostTouchRunnable); + } + } + private int predictFinalScrollPosition(int velocityX) { // predict where a fling would end up so we can scroll to the nearest snap offset final int maximumOffset = Math.max(0, computeHorizontalScrollRange() - getWidth()); diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java index 4c832ab6f09..3fbfb7a4d52 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java @@ -369,7 +369,7 @@ public class ReactScrollView extends ScrollView } mVelocityHelper.calculateVelocity(ev); - int action = ev.getAction() & MotionEvent.ACTION_MASK; + int action = ev.getActionMasked(); if (action == MotionEvent.ACTION_UP && mDragging) { ReactScrollViewHelper.updateFabricScrollState(this); @@ -382,6 +382,10 @@ public class ReactScrollView extends ScrollView handlePostTouchScrolling(Math.round(velocityX), Math.round(velocityY)); } + if (action == MotionEvent.ACTION_DOWN) { + cancelPostTouchScrolling(); + } + return super.onTouchEvent(ev); } @@ -611,6 +615,12 @@ public class ReactScrollView extends ScrollView this, mPostTouchRunnable, ReactScrollViewHelper.MOMENTUM_DELAY); } + private void cancelPostTouchScrolling() { + if (mPostTouchRunnable != null) { + removeCallbacks(mPostTouchRunnable); + } + } + private int predictFinalScrollPosition(int velocityY) { // predict where a fling would end up so we can scroll to the nearest snap offset // TODO(T106335409): Existing prediction still uses overscroller. Consider change this to use