Skip to content

Commit 963f7a5

Browse files
Abbondanzometa-codesync[bot]
authored andcommitted
Maintain momentum through non-zero velocity when paging is enabled (#54413)
Summary: Pull Request resolved: #54413 We should treat `ACTION_SCROLL` events akin to momentum scrolling--they represent a scroll with non-zero velocity--but not as momentum events themselves--they don't have a real velocity after they've dispatched. When paging is enabled, we should run the same `flingAndSnap` logic and rely on this non-zero velocity to encourage scrolling to the next item in the direction of the scroll rather than immediately picking the nearest offset. The end effect of this change is that scrolling down will always go to the next item regardless of when scrolling stops. Scrolling up will always go to the previous item. Similar for horizontal scrolling per right/left scrolling. Changelog: [Internal] Reviewed By: necolas Differential Revision: D86151012 fbshipit-source-id: 14987beac931e3d47a4cc013f5d2a4ccb5c2b8bd
1 parent 3012aac commit 963f7a5

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,8 +849,13 @@ public boolean dispatchGenericMotionEvent(MotionEvent ev) {
849849
@Override
850850
public void run() {
851851
mPostTouchRunnable = null;
852-
// Trigger snap alignment now that scrolling has stopped
853-
handlePostTouchScrolling(0, 0);
852+
// +1/-1 velocity if scrolling right or left. This is to ensure that the
853+
// next/previous page is picked rather than sliding backwards to the current page
854+
int velocityX = (int) Math.signum(hScroll);
855+
if (mDisableIntervalMomentum) {
856+
velocityX = 0;
857+
}
858+
flingAndSnap(velocityX);
854859
}
855860
};
856861
postOnAnimationDelayed(mPostTouchRunnable, ReactScrollViewHelper.MOMENTUM_DELAY);

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,13 @@ public boolean dispatchGenericMotionEvent(MotionEvent ev) {
688688
@Override
689689
public void run() {
690690
mPostTouchRunnable = null;
691-
// Trigger snap alignment now that scrolling has stopped
692-
handlePostTouchScrolling(0, 0);
691+
// +1/-1 velocity if scrolling down or up. This is to ensure that the
692+
// next/previous page is picked rather than sliding backwards to the current page
693+
int velocityY = (int) -Math.signum(vScroll);
694+
if (mDisableIntervalMomentum) {
695+
velocityY = 0;
696+
}
697+
flingAndSnap(velocityY);
693698
}
694699
};
695700
postOnAnimationDelayed(mPostTouchRunnable, ReactScrollViewHelper.MOMENTUM_DELAY);

0 commit comments

Comments
 (0)