Skip to content

Commit c49d249

Browse files
wwalserfacebook-github-bot
authored andcommitted
Send scroll velocity data to Javascript on momentum scroll events.
Reviewed By: sahrens Differential Revision: D6643379 fbshipit-source-id: 70550274975ed7c2b43a3d668422102d0c115ba7
1 parent 33d710e commit c49d249

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,16 @@ public boolean onTouchEvent(MotionEvent ev) {
158158
mVelocityHelper.calculateVelocity(ev);
159159
int action = ev.getAction() & MotionEvent.ACTION_MASK;
160160
if (action == MotionEvent.ACTION_UP && mDragging) {
161+
float velocityX = mVelocityHelper.getXVelocity();
162+
float velocityY = mVelocityHelper.getYVelocity();
161163
ReactScrollViewHelper.emitScrollEndDragEvent(
162164
this,
163-
mVelocityHelper.getXVelocity(),
164-
mVelocityHelper.getYVelocity());
165+
velocityX,
166+
velocityY);
165167
mDragging = false;
166168
// After the touch finishes, we may need to do some scrolling afterwards either as a result
167169
// of a fling or because we need to page align the content
168-
handlePostTouchScrolling();
170+
handlePostTouchScrolling(Math.round(velocityX), Math.round(velocityY));
169171
}
170172

171173
return super.onTouchEvent(ev);
@@ -178,7 +180,7 @@ public void fling(int velocityX) {
178180
} else {
179181
super.fling(velocityX);
180182
}
181-
handlePostTouchScrolling();
183+
handlePostTouchScrolling(velocityX, 0);
182184
}
183185

184186
@Override
@@ -270,7 +272,7 @@ public void draw(Canvas canvas) {
270272
* runnable that checks if we scrolled in the last frame and if so assumes we are still scrolling.
271273
*/
272274
@TargetApi(16)
273-
private void handlePostTouchScrolling() {
275+
private void handlePostTouchScrolling(int velocityX, int velocityY) {
274276
// If we aren't going to do anything (send events or snap to page), we can early out.
275277
if (!mSendMomentumEvents && !mPagingEnabled && !isScrollPerfLoggingEnabled()) {
276278
return;
@@ -283,7 +285,7 @@ private void handlePostTouchScrolling() {
283285
}
284286

285287
if (mSendMomentumEvents) {
286-
ReactScrollViewHelper.emitScrollMomentumBeginEvent(this);
288+
ReactScrollViewHelper.emitScrollMomentumBeginEvent(this, velocityX, velocityY);
287289
}
288290

289291
mActivelyScrolling = false;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public void fling(int velocityY) {
279279
if (mSendMomentumEvents || isScrollPerfLoggingEnabled()) {
280280
mFlinging = true;
281281
enableFpsListener();
282-
ReactScrollViewHelper.emitScrollMomentumBeginEvent(this);
282+
ReactScrollViewHelper.emitScrollMomentumBeginEvent(this, 0, velocityY);
283283
Runnable r = new Runnable() {
284284
@Override
285285
public void run() {

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ public static void emitScrollEndDragEvent(
4444
emitScrollEvent(scrollView, ScrollEventType.END_DRAG, xVelocity, yVelocity);
4545
}
4646

47-
public static void emitScrollMomentumBeginEvent(ViewGroup scrollView) {
48-
emitScrollEvent(scrollView, ScrollEventType.MOMENTUM_BEGIN);
47+
public static void emitScrollMomentumBeginEvent(
48+
ViewGroup scrollView,
49+
int xVelocity,
50+
int yVelocity) {
51+
emitScrollEvent(scrollView, ScrollEventType.MOMENTUM_BEGIN, xVelocity, yVelocity);
4952
}
5053

5154
public static void emitScrollMomentumEndEvent(ViewGroup scrollView) {

0 commit comments

Comments
 (0)