From 82b0df9d2a6163879647691235f01b3067c471b8 Mon Sep 17 00:00:00 2001 From: Brent Vatne Date: Wed, 3 Feb 2016 13:38:17 -0800 Subject: [PATCH] Add scrollEnabled prop to Android ScrollView Summary: Perhaps there is a better way to do this, curious to hear it! - If momentum scroll is active when `scrollEnabled` is toggled, the momentum scroll continues and the onMomentumScrollEnd event fires, which is the same as on iOS. Closes https://github.com/facebook/react-native/pull/5656 Reviewed By: svcscm Differential Revision: D2889897 Pulled By: dmmiller fb-gh-sync-id: b2f44d2bcb48373f9945f6afd966447a118df717 --- Libraries/Components/ScrollView/ScrollView.js | 1 - .../scroll/ReactHorizontalScrollView.java | 22 ++++++++++++++----- .../ReactHorizontalScrollViewManager.java | 12 ++++++---- .../react/views/scroll/ReactScrollView.java | 19 +++++++++++++--- .../views/scroll/ReactScrollViewManager.java | 5 +++++ 5 files changed, 46 insertions(+), 13 deletions(-) diff --git a/Libraries/Components/ScrollView/ScrollView.js b/Libraries/Components/ScrollView/ScrollView.js index d9d710246..bd3f9b99e 100644 --- a/Libraries/Components/ScrollView/ScrollView.js +++ b/Libraries/Components/ScrollView/ScrollView.js @@ -227,7 +227,6 @@ var ScrollView = React.createClass({ /** * When false, the content does not scroll. * The default value is true. - * @platform ios */ scrollEnabled: PropTypes.bool, /** diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java index 61735c0ea..e35dd0647 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java @@ -31,13 +31,13 @@ public class ReactHorizontalScrollView extends HorizontalScrollView implements private final OnScrollDispatchHelper mOnScrollDispatchHelper = new OnScrollDispatchHelper(); - private boolean mRemoveClippedSubviews; - private boolean mSendMomentumEvents; + private @Nullable Rect mClippingRect; + private boolean mDoneFlinging; private boolean mDragging; private boolean mFlinging; - private boolean mDoneFlinging; - - private @Nullable Rect mClippingRect; + private boolean mRemoveClippedSubviews; + private boolean mScrollEnabled = true; + private boolean mSendMomentumEvents; public ReactHorizontalScrollView(Context context) { super(context); @@ -61,6 +61,10 @@ public class ReactHorizontalScrollView extends HorizontalScrollView implements mSendMomentumEvents = sendMomentumEvents; } + public void setScrollEnabled(boolean scrollEnabled) { + mScrollEnabled = scrollEnabled; + } + @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { MeasureSpecAssertions.assertExplicitMeasureSpec(widthMeasureSpec, heightMeasureSpec); @@ -95,6 +99,10 @@ public class ReactHorizontalScrollView extends HorizontalScrollView implements @Override public boolean onInterceptTouchEvent(MotionEvent ev) { + if (!mScrollEnabled) { + return false; + } + if (super.onInterceptTouchEvent(ev)) { NativeGestureUtil.notifyNativeGestureStarted(this, ev); ReactScrollViewHelper.emitScrollBeginDragEvent(this); @@ -107,6 +115,10 @@ public class ReactHorizontalScrollView extends HorizontalScrollView implements @Override public boolean onTouchEvent(MotionEvent ev) { + if (!mScrollEnabled) { + return false; + } + int action = ev.getAction() & MotionEvent.ACTION_MASK; if (action == MotionEvent.ACTION_UP && mDragging) { ReactScrollViewHelper.emitScrollEndDragEvent(this); diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java index 923d35e77..69185fef8 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java @@ -39,17 +39,21 @@ public class ReactHorizontalScrollViewManager return new ReactHorizontalScrollView(context); } - @ReactProp(name = ReactClippingViewGroupHelper.PROP_REMOVE_CLIPPED_SUBVIEWS) - public void setRemoveClippedSubviews(ReactHorizontalScrollView view, boolean removeClippedSubviews) { - view.setRemoveClippedSubviews(removeClippedSubviews); + @ReactProp(name = "scrollEnabled", defaultBoolean = true) + public void setScrollEnabled(ReactHorizontalScrollView view, boolean value) { + view.setScrollEnabled(value); } - @ReactProp(name = "showsHorizontalScrollIndicator") public void setShowsHorizontalScrollIndicator(ReactHorizontalScrollView view, boolean value) { view.setHorizontalScrollBarEnabled(value); } + @ReactProp(name = ReactClippingViewGroupHelper.PROP_REMOVE_CLIPPED_SUBVIEWS) + public void setRemoveClippedSubviews(ReactHorizontalScrollView view, boolean removeClippedSubviews) { + view.setRemoveClippedSubviews(removeClippedSubviews); + } + /** * Computing momentum events is potentially expensive since we post a runnable on the UI thread * to see when it is done. We only do that if {@param sendMomentumEvents} is set to true. This diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java index 64559d1a3..5e87db3bf 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java @@ -34,12 +34,13 @@ public class ReactScrollView extends ScrollView implements ReactClippingViewGrou private final OnScrollDispatchHelper mOnScrollDispatchHelper = new OnScrollDispatchHelper(); - private boolean mRemoveClippedSubviews; private @Nullable Rect mClippingRect; - private boolean mSendMomentumEvents; + private boolean mDoneFlinging; private boolean mDragging; private boolean mFlinging; - private boolean mDoneFlinging; + private boolean mRemoveClippedSubviews; + private boolean mScrollEnabled = true; + private boolean mSendMomentumEvents; public ReactScrollView(Context context) { super(context); @@ -49,6 +50,10 @@ public class ReactScrollView extends ScrollView implements ReactClippingViewGrou mSendMomentumEvents = sendMomentumEvents; } + public void setScrollEnabled(boolean scrollEnabled) { + mScrollEnabled = scrollEnabled; + } + @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { MeasureSpecAssertions.assertExplicitMeasureSpec(widthMeasureSpec, heightMeasureSpec); @@ -99,6 +104,10 @@ public class ReactScrollView extends ScrollView implements ReactClippingViewGrou @Override public boolean onInterceptTouchEvent(MotionEvent ev) { + if (!mScrollEnabled) { + return false; + } + if (super.onInterceptTouchEvent(ev)) { NativeGestureUtil.notifyNativeGestureStarted(this, ev); ReactScrollViewHelper.emitScrollBeginDragEvent(this); @@ -111,6 +120,10 @@ public class ReactScrollView extends ScrollView implements ReactClippingViewGrou @Override public boolean onTouchEvent(MotionEvent ev) { + if (!mScrollEnabled) { + return false; + } + int action = ev.getAction() & MotionEvent.ACTION_MASK; if (action == MotionEvent.ACTION_UP && mDragging) { ReactScrollViewHelper.emitScrollEndDragEvent(this); diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java index ff71d30ab..be023a76a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java @@ -42,6 +42,11 @@ public class ReactScrollViewManager return new ReactScrollView(context); } + @ReactProp(name = "scrollEnabled", defaultBoolean = true) + public void setScrollEnabled(ReactScrollView view, boolean value) { + view.setScrollEnabled(value); + } + @ReactProp(name = "showsVerticalScrollIndicator") public void setShowsVerticalScrollIndicator(ReactScrollView view, boolean value) { view.setVerticalScrollBarEnabled(value);