Revert D5238126: Add velocity to onScrollEndDrag event
Differential Revision: D5238126 fbshipit-source-id: a8b5f9445fa15a0ef0851303961dbf63b5d46623
This commit is contained in:
Родитель
e2628f93b5
Коммит
076eaec805
|
@ -22,13 +22,8 @@ public class OnScrollDispatchHelper {
|
||||||
|
|
||||||
private int mPrevX = Integer.MIN_VALUE;
|
private int mPrevX = Integer.MIN_VALUE;
|
||||||
private int mPrevY = Integer.MIN_VALUE;
|
private int mPrevY = Integer.MIN_VALUE;
|
||||||
private float mXFlingVelocity = 0;
|
|
||||||
private float mYFlingVelocity = 0;
|
|
||||||
|
|
||||||
private long mLastScrollEventTimeMs = -(MIN_EVENT_SEPARATION_MS + 1);
|
private long mLastScrollEventTimeMs = -(MIN_EVENT_SEPARATION_MS + 1);
|
||||||
|
|
||||||
private static final float THRESHOLD = 0.1f; // Threshold for end fling
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Call from a ScrollView in onScrollChanged, returns true if this onScrollChanged is legit (not a
|
* Call from a ScrollView in onScrollChanged, returns true if this onScrollChanged is legit (not a
|
||||||
* duplicate) and should be dispatched.
|
* duplicate) and should be dispatched.
|
||||||
|
@ -40,28 +35,10 @@ public class OnScrollDispatchHelper {
|
||||||
mPrevX != x ||
|
mPrevX != x ||
|
||||||
mPrevY != y;
|
mPrevY != y;
|
||||||
|
|
||||||
// Skip the first calculation in each scroll
|
|
||||||
if (Math.abs(mXFlingVelocity) < THRESHOLD && Math.abs(mYFlingVelocity) < THRESHOLD) {
|
|
||||||
shouldDispatch = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (eventTime - mLastScrollEventTimeMs != 0) {
|
|
||||||
mXFlingVelocity = (float) (x - mPrevX) / (eventTime - mLastScrollEventTimeMs);
|
|
||||||
mYFlingVelocity = (float) (y - mPrevY) / (eventTime - mLastScrollEventTimeMs);
|
|
||||||
}
|
|
||||||
|
|
||||||
mLastScrollEventTimeMs = eventTime;
|
mLastScrollEventTimeMs = eventTime;
|
||||||
mPrevX = x;
|
mPrevX = x;
|
||||||
mPrevY = y;
|
mPrevY = y;
|
||||||
|
|
||||||
return shouldDispatch;
|
return shouldDispatch;
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getXFlingVelocity() {
|
|
||||||
return this.mXFlingVelocity;
|
|
||||||
}
|
|
||||||
|
|
||||||
public float getYFlingVelocity() {
|
|
||||||
return this.mYFlingVelocity;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,6 @@ public class ReactHorizontalScrollView extends HorizontalScrollView implements
|
||||||
ReactClippingViewGroup {
|
ReactClippingViewGroup {
|
||||||
|
|
||||||
private final OnScrollDispatchHelper mOnScrollDispatchHelper = new OnScrollDispatchHelper();
|
private final OnScrollDispatchHelper mOnScrollDispatchHelper = new OnScrollDispatchHelper();
|
||||||
private final VelocityHelper mVelocityHelper = new VelocityHelper();
|
|
||||||
|
|
||||||
private boolean mActivelyScrolling;
|
private boolean mActivelyScrolling;
|
||||||
private @Nullable Rect mClippingRect;
|
private @Nullable Rect mClippingRect;
|
||||||
|
@ -118,10 +117,7 @@ public class ReactHorizontalScrollView extends HorizontalScrollView implements
|
||||||
|
|
||||||
mActivelyScrolling = true;
|
mActivelyScrolling = true;
|
||||||
|
|
||||||
ReactScrollViewHelper.emitScrollEvent(
|
ReactScrollViewHelper.emitScrollEvent(this);
|
||||||
this,
|
|
||||||
mOnScrollDispatchHelper.getXFlingVelocity(),
|
|
||||||
mOnScrollDispatchHelper.getYFlingVelocity());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,19 +144,14 @@ public class ReactHorizontalScrollView extends HorizontalScrollView implements
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
mVelocityHelper.calculateVelocity(ev);
|
|
||||||
int action = ev.getAction() & MotionEvent.ACTION_MASK;
|
int action = ev.getAction() & MotionEvent.ACTION_MASK;
|
||||||
if (action == MotionEvent.ACTION_UP && mDragging) {
|
if (action == MotionEvent.ACTION_UP && mDragging) {
|
||||||
ReactScrollViewHelper.emitScrollEndDragEvent(
|
ReactScrollViewHelper.emitScrollEndDragEvent(this);
|
||||||
this,
|
|
||||||
mVelocityHelper.getXVelocity(),
|
|
||||||
mVelocityHelper.getYVelocity());
|
|
||||||
mDragging = false;
|
mDragging = false;
|
||||||
// After the touch finishes, we may need to do some scrolling afterwards either as a result
|
// 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
|
// of a fling or because we need to page align the content
|
||||||
handlePostTouchScrolling();
|
handlePostTouchScrolling();
|
||||||
}
|
}
|
||||||
|
|
||||||
return super.onTouchEvent(ev);
|
return super.onTouchEvent(ev);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,6 @@ public class ReactScrollView extends ScrollView implements ReactClippingViewGrou
|
||||||
|
|
||||||
private final OnScrollDispatchHelper mOnScrollDispatchHelper = new OnScrollDispatchHelper();
|
private final OnScrollDispatchHelper mOnScrollDispatchHelper = new OnScrollDispatchHelper();
|
||||||
private final OverScroller mScroller;
|
private final OverScroller mScroller;
|
||||||
private final VelocityHelper mVelocityHelper = new VelocityHelper();
|
|
||||||
|
|
||||||
private @Nullable Rect mClippingRect;
|
private @Nullable Rect mClippingRect;
|
||||||
private boolean mDoneFlinging;
|
private boolean mDoneFlinging;
|
||||||
|
@ -165,10 +164,7 @@ public class ReactScrollView extends ScrollView implements ReactClippingViewGrou
|
||||||
mDoneFlinging = false;
|
mDoneFlinging = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ReactScrollViewHelper.emitScrollEvent(
|
ReactScrollViewHelper.emitScrollEvent(this);
|
||||||
this,
|
|
||||||
mOnScrollDispatchHelper.getXFlingVelocity(),
|
|
||||||
mOnScrollDispatchHelper.getYFlingVelocity());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,17 +191,12 @@ public class ReactScrollView extends ScrollView implements ReactClippingViewGrou
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
mVelocityHelper.calculateVelocity(ev);
|
|
||||||
int action = ev.getAction() & MotionEvent.ACTION_MASK;
|
int action = ev.getAction() & MotionEvent.ACTION_MASK;
|
||||||
if (action == MotionEvent.ACTION_UP && mDragging) {
|
if (action == MotionEvent.ACTION_UP && mDragging) {
|
||||||
ReactScrollViewHelper.emitScrollEndDragEvent(
|
ReactScrollViewHelper.emitScrollEndDragEvent(this);
|
||||||
this,
|
|
||||||
mVelocityHelper.getXVelocity(),
|
|
||||||
mVelocityHelper.getYVelocity());
|
|
||||||
mDragging = false;
|
mDragging = false;
|
||||||
disableFpsListener();
|
disableFpsListener();
|
||||||
}
|
}
|
||||||
|
|
||||||
return super.onTouchEvent(ev);
|
return super.onTouchEvent(ev);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,19 +29,16 @@ public class ReactScrollViewHelper {
|
||||||
/**
|
/**
|
||||||
* Shared by {@link ReactScrollView} and {@link ReactHorizontalScrollView}.
|
* Shared by {@link ReactScrollView} and {@link ReactHorizontalScrollView}.
|
||||||
*/
|
*/
|
||||||
public static void emitScrollEvent(ViewGroup scrollView, float xVelocity, float yVelocity) {
|
public static void emitScrollEvent(ViewGroup scrollView) {
|
||||||
emitScrollEvent(scrollView, ScrollEventType.SCROLL, xVelocity, yVelocity);
|
emitScrollEvent(scrollView, ScrollEventType.SCROLL);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void emitScrollBeginDragEvent(ViewGroup scrollView) {
|
public static void emitScrollBeginDragEvent(ViewGroup scrollView) {
|
||||||
emitScrollEvent(scrollView, ScrollEventType.BEGIN_DRAG);
|
emitScrollEvent(scrollView, ScrollEventType.BEGIN_DRAG);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void emitScrollEndDragEvent(
|
public static void emitScrollEndDragEvent(ViewGroup scrollView) {
|
||||||
ViewGroup scrollView,
|
emitScrollEvent(scrollView, ScrollEventType.END_DRAG);
|
||||||
float xVelocity,
|
|
||||||
float yVelocity) {
|
|
||||||
emitScrollEvent(scrollView, ScrollEventType.END_DRAG, xVelocity, yVelocity);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void emitScrollMomentumBeginEvent(ViewGroup scrollView) {
|
public static void emitScrollMomentumBeginEvent(ViewGroup scrollView) {
|
||||||
|
@ -53,14 +50,6 @@ public class ReactScrollViewHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void emitScrollEvent(ViewGroup scrollView, ScrollEventType scrollEventType) {
|
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);
|
View contentView = scrollView.getChildAt(0);
|
||||||
|
|
||||||
if (contentView == null) {
|
if (contentView == null) {
|
||||||
|
@ -74,8 +63,6 @@ public class ReactScrollViewHelper {
|
||||||
scrollEventType,
|
scrollEventType,
|
||||||
scrollView.getScrollX(),
|
scrollView.getScrollX(),
|
||||||
scrollView.getScrollY(),
|
scrollView.getScrollY(),
|
||||||
xVelocity,
|
|
||||||
yVelocity,
|
|
||||||
contentView.getWidth(),
|
contentView.getWidth(),
|
||||||
contentView.getHeight(),
|
contentView.getHeight(),
|
||||||
scrollView.getWidth(),
|
scrollView.getWidth(),
|
||||||
|
|
|
@ -32,8 +32,6 @@ public class ScrollEvent extends Event<ScrollEvent> {
|
||||||
|
|
||||||
private int mScrollX;
|
private int mScrollX;
|
||||||
private int mScrollY;
|
private int mScrollY;
|
||||||
private double mXVelocity;
|
|
||||||
private double mYVelocity;
|
|
||||||
private int mContentWidth;
|
private int mContentWidth;
|
||||||
private int mContentHeight;
|
private int mContentHeight;
|
||||||
private int mScrollViewWidth;
|
private int mScrollViewWidth;
|
||||||
|
@ -45,8 +43,6 @@ public class ScrollEvent extends Event<ScrollEvent> {
|
||||||
ScrollEventType scrollEventType,
|
ScrollEventType scrollEventType,
|
||||||
int scrollX,
|
int scrollX,
|
||||||
int scrollY,
|
int scrollY,
|
||||||
float xVelocity,
|
|
||||||
float yVelocity,
|
|
||||||
int contentWidth,
|
int contentWidth,
|
||||||
int contentHeight,
|
int contentHeight,
|
||||||
int scrollViewWidth,
|
int scrollViewWidth,
|
||||||
|
@ -60,8 +56,6 @@ public class ScrollEvent extends Event<ScrollEvent> {
|
||||||
scrollEventType,
|
scrollEventType,
|
||||||
scrollX,
|
scrollX,
|
||||||
scrollY,
|
scrollY,
|
||||||
xVelocity,
|
|
||||||
yVelocity,
|
|
||||||
contentWidth,
|
contentWidth,
|
||||||
contentHeight,
|
contentHeight,
|
||||||
scrollViewWidth,
|
scrollViewWidth,
|
||||||
|
@ -82,8 +76,6 @@ public class ScrollEvent extends Event<ScrollEvent> {
|
||||||
ScrollEventType scrollEventType,
|
ScrollEventType scrollEventType,
|
||||||
int scrollX,
|
int scrollX,
|
||||||
int scrollY,
|
int scrollY,
|
||||||
float xVelocity,
|
|
||||||
float yVelocity,
|
|
||||||
int contentWidth,
|
int contentWidth,
|
||||||
int contentHeight,
|
int contentHeight,
|
||||||
int scrollViewWidth,
|
int scrollViewWidth,
|
||||||
|
@ -92,8 +84,6 @@ public class ScrollEvent extends Event<ScrollEvent> {
|
||||||
mScrollEventType = scrollEventType;
|
mScrollEventType = scrollEventType;
|
||||||
mScrollX = scrollX;
|
mScrollX = scrollX;
|
||||||
mScrollY = scrollY;
|
mScrollY = scrollY;
|
||||||
mXVelocity = xVelocity;
|
|
||||||
mYVelocity = yVelocity;
|
|
||||||
mContentWidth = contentWidth;
|
mContentWidth = contentWidth;
|
||||||
mContentHeight = contentHeight;
|
mContentHeight = contentHeight;
|
||||||
mScrollViewWidth = scrollViewWidth;
|
mScrollViewWidth = scrollViewWidth;
|
||||||
|
@ -144,16 +134,11 @@ public class ScrollEvent extends Event<ScrollEvent> {
|
||||||
layoutMeasurement.putDouble("width", PixelUtil.toDIPFromPixel(mScrollViewWidth));
|
layoutMeasurement.putDouble("width", PixelUtil.toDIPFromPixel(mScrollViewWidth));
|
||||||
layoutMeasurement.putDouble("height", PixelUtil.toDIPFromPixel(mScrollViewHeight));
|
layoutMeasurement.putDouble("height", PixelUtil.toDIPFromPixel(mScrollViewHeight));
|
||||||
|
|
||||||
WritableMap velocity = Arguments.createMap();
|
|
||||||
velocity.putDouble("x", mXVelocity);
|
|
||||||
velocity.putDouble("y", mYVelocity);
|
|
||||||
|
|
||||||
WritableMap event = Arguments.createMap();
|
WritableMap event = Arguments.createMap();
|
||||||
event.putMap("contentInset", contentInset);
|
event.putMap("contentInset", contentInset);
|
||||||
event.putMap("contentOffset", contentOffset);
|
event.putMap("contentOffset", contentOffset);
|
||||||
event.putMap("contentSize", contentSize);
|
event.putMap("contentSize", contentSize);
|
||||||
event.putMap("layoutMeasurement", layoutMeasurement);
|
event.putMap("layoutMeasurement", layoutMeasurement);
|
||||||
event.putMap("velocity", velocity);
|
|
||||||
|
|
||||||
event.putInt("target", getViewTag());
|
event.putInt("target", getViewTag());
|
||||||
event.putBoolean("responderIgnoreScroll", true);
|
event.putBoolean("responderIgnoreScroll", true);
|
||||||
|
|
|
@ -1,62 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright (c) 2017-present, Facebook, Inc.
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the BSD-style license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree. An additional grant
|
|
||||||
* of patent rights can be found in the PATENTS file in the same directory.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.facebook.react.views.scroll;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
|
|
||||||
import android.view.MotionEvent;
|
|
||||||
import android.view.VelocityTracker;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This Class helps to calculate the velocity for all ScrollView. The x and y velocity
|
|
||||||
* will later on send to ReactScrollViewHelper for further use.
|
|
||||||
*
|
|
||||||
* Created by wenjiam on 6/20/17.
|
|
||||||
*/
|
|
||||||
public class VelocityHelper {
|
|
||||||
|
|
||||||
private @Nullable VelocityTracker mVelocityTracker;
|
|
||||||
private float mXVelocity;
|
|
||||||
private float mYVelocity;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Call from a ScrollView in onTouchEvent.
|
|
||||||
* Calculating the velocity for END_DRAG movement and send them back to react ScrollResponder.js
|
|
||||||
* */
|
|
||||||
public void calculateVelocity(MotionEvent ev) {
|
|
||||||
int action = ev.getAction() & MotionEvent.ACTION_MASK;
|
|
||||||
if (mVelocityTracker == null) {
|
|
||||||
mVelocityTracker = mVelocityTracker.obtain();
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (action) {
|
|
||||||
case MotionEvent.ACTION_MOVE: {
|
|
||||||
mVelocityTracker.addMovement(ev);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case MotionEvent.ACTION_UP:
|
|
||||||
case MotionEvent.ACTION_CANCEL: {
|
|
||||||
// Calculate velocity on END_DRAG
|
|
||||||
mVelocityTracker.computeCurrentVelocity(1); // points/millisecond
|
|
||||||
mXVelocity = mVelocityTracker.getXVelocity();
|
|
||||||
mYVelocity = mVelocityTracker.getYVelocity();
|
|
||||||
|
|
||||||
mVelocityTracker.recycle();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Needs to call ACTION_UP/CANCEL to update the mXVelocity */
|
|
||||||
public float getXVelocity() { return mXVelocity; }
|
|
||||||
|
|
||||||
/* Needs to call ACTION_UP/CANCEL to update the mYVelocity */
|
|
||||||
public float getYVelocity() { return mYVelocity; }
|
|
||||||
}
|
|
|
@ -836,12 +836,11 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
|
||||||
ScrollEventType.SCROLL,
|
ScrollEventType.SCROLL,
|
||||||
horiz,
|
horiz,
|
||||||
vert,
|
vert,
|
||||||
0f, // can't get x velocity
|
|
||||||
0f, // can't get y velocity
|
|
||||||
0, // can't get content width
|
0, // can't get content width
|
||||||
0, // can't get content height
|
0, // can't get content height
|
||||||
mReactEditText.getWidth(),
|
mReactEditText.getWidth(),
|
||||||
mReactEditText.getHeight());
|
mReactEditText.getHeight()
|
||||||
|
);
|
||||||
|
|
||||||
mEventDispatcher.dispatchEvent(event);
|
mEventDispatcher.dispatchEvent(event);
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче