Skip to content

Commit

Permalink
Send scroll velocity data to Javascript on momentum scroll events.
Browse files Browse the repository at this point in the history
Reviewed By: sahrens

Differential Revision: D6643379

fbshipit-source-id: 70550274975ed7c2b43a3d668422102d0c115ba7
  • Loading branch information
wwalser authored and facebook-github-bot committed Jan 5, 2018
1 parent 33d710e commit c49d249
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,16 @@ public boolean onTouchEvent(MotionEvent ev) {
mVelocityHelper.calculateVelocity(ev);
int action = ev.getAction() & MotionEvent.ACTION_MASK;
if (action == MotionEvent.ACTION_UP && mDragging) {
float velocityX = mVelocityHelper.getXVelocity();
float velocityY = mVelocityHelper.getYVelocity();
ReactScrollViewHelper.emitScrollEndDragEvent(
this,
mVelocityHelper.getXVelocity(),
mVelocityHelper.getYVelocity());
velocityX,
velocityY);
mDragging = false;
// After the touch finishes, we may need to do some scrolling afterwards either as a result
// of a fling or because we need to page align the content
handlePostTouchScrolling();
handlePostTouchScrolling(Math.round(velocityX), Math.round(velocityY));
}

return super.onTouchEvent(ev);
Expand All @@ -178,7 +180,7 @@ public void fling(int velocityX) {
} else {
super.fling(velocityX);
}
handlePostTouchScrolling();
handlePostTouchScrolling(velocityX, 0);
}

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

if (mSendMomentumEvents) {
ReactScrollViewHelper.emitScrollMomentumBeginEvent(this);
ReactScrollViewHelper.emitScrollMomentumBeginEvent(this, velocityX, velocityY);
}

mActivelyScrolling = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public void fling(int velocityY) {
if (mSendMomentumEvents || isScrollPerfLoggingEnabled()) {
mFlinging = true;
enableFpsListener();
ReactScrollViewHelper.emitScrollMomentumBeginEvent(this);
ReactScrollViewHelper.emitScrollMomentumBeginEvent(this, 0, velocityY);
Runnable r = new Runnable() {
@Override
public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ public static void emitScrollEndDragEvent(
emitScrollEvent(scrollView, ScrollEventType.END_DRAG, xVelocity, yVelocity);
}

public static void emitScrollMomentumBeginEvent(ViewGroup scrollView) {
emitScrollEvent(scrollView, ScrollEventType.MOMENTUM_BEGIN);
public static void emitScrollMomentumBeginEvent(
ViewGroup scrollView,
int xVelocity,
int yVelocity) {
emitScrollEvent(scrollView, ScrollEventType.MOMENTUM_BEGIN, xVelocity, yVelocity);
}

public static void emitScrollMomentumEndEvent(ViewGroup scrollView) {
Expand Down

0 comments on commit c49d249

Please sign in to comment.