diff --git a/android/src/main/java/com/mohtada/nestedscrollview/ReactNestedScrollView.java b/android/src/main/java/com/mohtada/nestedscrollview/ReactNestedScrollView.java index 136bf85..d479676 100644 --- a/android/src/main/java/com/mohtada/nestedscrollview/ReactNestedScrollView.java +++ b/android/src/main/java/com/mohtada/nestedscrollview/ReactNestedScrollView.java @@ -29,6 +29,7 @@ import com.facebook.react.common.ReactConstants; import com.facebook.react.views.scroll.FpsListener; import com.facebook.react.views.scroll.OnScrollDispatchHelper; +import com.facebook.react.views.scroll.VelocityHelper; import com.facebook.react.uimanager.MeasureSpecAssertions; import com.facebook.react.uimanager.events.NativeGestureUtil; import com.facebook.react.uimanager.ReactClippingViewGroup; @@ -53,6 +54,7 @@ public class ReactNestedScrollView extends NestedScrollView implements ReactClip private final OnScrollDispatchHelper mOnScrollDispatchHelper = new OnScrollDispatchHelper(); private final OverScroller mScroller; + private final VelocityHelper mVelocityHelper = new VelocityHelper(); private @Nullable Rect mClippingRect; private boolean mDoneFlinging; @@ -153,20 +155,23 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) { // } @Override - protected void onScrollChanged(int x, int y, int oldX, int oldY) { + protected void onScrollChanged(int x, int y, int oldX, int oldY) { super.onScrollChanged(x, y, oldX, oldY); if (mOnScrollDispatchHelper.onScrollChanged(x, y)) { - if (mRemoveClippedSubviews) { - updateClippingRect(); - } + if (mRemoveClippedSubviews) { + updateClippingRect(); + } - if (mFlinging) { - mDoneFlinging = false; - } + if (mFlinging) { + mDoneFlinging = false; + } - ReactNestedScrollViewHelper.emitScrollEvent(this); - } + ReactNestedScrollViewHelper.emitScrollEvent( + this, + mOnScrollDispatchHelper.getXFlingVelocity(), + mOnScrollDispatchHelper.getYFlingVelocity()); + } } @Override @@ -187,17 +192,22 @@ public boolean onInterceptTouchEvent(MotionEvent ev) { } @Override - public boolean onTouchEvent(MotionEvent ev) { + public boolean onTouchEvent(MotionEvent ev) { if (!mScrollEnabled) { - return false; + return false; } + mVelocityHelper.calculateVelocity(ev); int action = ev.getAction() & MotionEvent.ACTION_MASK; if (action == MotionEvent.ACTION_UP && mDragging) { - ReactNestedScrollViewHelper.emitScrollEndDragEvent(this); - mDragging = false; - disableFpsListener(); + ReactNestedScrollViewHelper.emitScrollEndDragEvent( + this, + mVelocityHelper.getXVelocity(), + mVelocityHelper.getYVelocity()); + mDragging = false; + disableFpsListener(); } + return super.onTouchEvent(ev); } diff --git a/android/src/main/java/com/mohtada/nestedscrollview/ReactNestedScrollViewHelper.java b/android/src/main/java/com/mohtada/nestedscrollview/ReactNestedScrollViewHelper.java index d501b2f..5d911fa 100644 --- a/android/src/main/java/com/mohtada/nestedscrollview/ReactNestedScrollViewHelper.java +++ b/android/src/main/java/com/mohtada/nestedscrollview/ReactNestedScrollViewHelper.java @@ -31,34 +31,46 @@ public class ReactNestedScrollViewHelper { public static final String AUTO = "auto"; public static final String OVER_SCROLL_NEVER = "never"; + /** * Shared by {@link ReactScrollView} and {@link ReactHorizontalScrollView}. */ - public static void emitScrollEvent(ViewGroup scrollView) { - emitScrollEvent(scrollView, ScrollEventType.SCROLL); + public static void emitScrollEvent(ViewGroup scrollView, float xVelocity, float yVelocity) { + emitScrollEvent(scrollView, ScrollEventType.SCROLL, xVelocity, yVelocity); } public static void emitScrollBeginDragEvent(ViewGroup scrollView) { - emitScrollEvent(scrollView, ScrollEventType.BEGIN_DRAG); + emitScrollEvent(scrollView, ScrollEventType.BEGIN_DRAG); } - public static void emitScrollEndDragEvent(ViewGroup scrollView) { - emitScrollEvent(scrollView, ScrollEventType.END_DRAG); + public static void emitScrollEndDragEvent( + ViewGroup scrollView, + float xVelocity, + float yVelocity) { + emitScrollEvent(scrollView, ScrollEventType.END_DRAG, xVelocity, yVelocity); } public static void emitScrollMomentumBeginEvent(ViewGroup scrollView) { - emitScrollEvent(scrollView, ScrollEventType.MOMENTUM_BEGIN); + emitScrollEvent(scrollView, ScrollEventType.MOMENTUM_BEGIN); } public static void emitScrollMomentumEndEvent(ViewGroup scrollView) { - emitScrollEvent(scrollView, ScrollEventType.MOMENTUM_END); + emitScrollEvent(scrollView, ScrollEventType.MOMENTUM_END); } private static void emitScrollEvent(ViewGroup scrollView, ScrollEventType scrollEventType) { + emitScrollEvent(scrollView, scrollEventType, 0, 0); + } + + private static void emitScrollEvent( + ViewGroup scrollView, + ScrollEventType scrollEventType, + float xVelocity, + float yVelocity) { View contentView = scrollView.getChildAt(0); if (contentView == null) { - return; + return; } ReactContext reactContext = (ReactContext) scrollView.getContext(); @@ -68,6 +80,8 @@ private static void emitScrollEvent(ViewGroup scrollView, ScrollEventType scroll scrollEventType, scrollView.getScrollX(), scrollView.getScrollY(), + xVelocity, + yVelocity, contentView.getWidth(), contentView.getHeight(), scrollView.getWidth(), diff --git a/android/src/main/java/com/mohtada/nestedscrollview/ReactNestedScrollViewPackage.java b/android/src/main/java/com/mohtada/nestedscrollview/ReactNestedScrollViewPackage.java index eff85af..2aca59f 100644 --- a/android/src/main/java/com/mohtada/nestedscrollview/ReactNestedScrollViewPackage.java +++ b/android/src/main/java/com/mohtada/nestedscrollview/ReactNestedScrollViewPackage.java @@ -16,10 +16,6 @@ public List createNativeModules(ReactApplicationContext reactAppli return Collections.emptyList(); } - @Override - public List> createJSModules() { - return Collections.emptyList(); - } @Override public List createViewManagers(ReactApplicationContext reactApplicationContext) {