From 12c486862896ca2391577410656dfc4580361fef Mon Sep 17 00:00:00 2001 From: Adam Comella Date: Thu, 2 Feb 2017 04:13:20 -0800 Subject: [PATCH] Android: Add overScrollMode prop to ScrollView Summary: This prop exposes the functionality provided by Android ScrollView's setOverScrollMode method. One interesting thing to note is that, if you were to read the Android docs, you would think that the value "always" is the default over scroll mode. However, the docs are incorrect and "always-if-content-scrolls" is actually the default value (http://stackoverflow.com/a/27116306). **Test plan (required)** Verified this change in a test app. Also, our team uses this change in our app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/10905 Differential Revision: D4500957 Pulled By: mkonicek fbshipit-source-id: 873eba38183defba133c228e0c1038efa83297d3 --- Libraries/Components/ScrollView/ScrollView.js | 18 ++++++++++++++++++ .../ReactHorizontalScrollViewManager.java | 9 +++++++++ .../views/scroll/ReactScrollViewHelper.java | 16 ++++++++++++++++ .../views/scroll/ReactScrollViewManager.java | 9 +++++++++ 4 files changed, 52 insertions(+) diff --git a/Libraries/Components/ScrollView/ScrollView.js b/Libraries/Components/ScrollView/ScrollView.js index 1c7b704c8..afc4306d2 100644 --- a/Libraries/Components/ScrollView/ScrollView.js +++ b/Libraries/Components/ScrollView/ScrollView.js @@ -349,6 +349,24 @@ const ScrollView = React.createClass({ * @platform android */ scrollPerfTag: PropTypes.string, + + /** + * Used to override default value of overScroll mode. + * + * Possible values: + * + * - `'auto'` - Default value, allow a user to over-scroll + * this view only if the content is large enough to meaningfully scroll. + * - `'always'` - Always allow a user to over-scroll this view. + * - `'never'` - Never allow a user to over-scroll this view. + * + * @platform android + */ + overScrollMode: PropTypes.oneOf([ + 'auto', + 'always', + 'never', + ]), }, mixins: [ScrollResponder.Mixin], 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 769a90738..2b0fe746e 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 @@ -12,6 +12,7 @@ package com.facebook.react.views.scroll; import javax.annotation.Nullable; import android.graphics.Color; +import android.view.View; import com.facebook.react.bridge.ReadableArray; import com.facebook.react.module.annotations.ReactModule; @@ -98,6 +99,14 @@ public class ReactHorizontalScrollViewManager view.setPagingEnabled(pagingEnabled); } + /** + * Controls overScroll behaviour + */ + @ReactProp(name = "overScrollMode") + public void setOverScrollMode(ReactHorizontalScrollView view, String value) { + view.setOverScrollMode(ReactScrollViewHelper.parseOverScrollMode(value)); + } + @Override public void receiveCommand( ReactHorizontalScrollView scrollView, diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.java index 966c50bb3..567cf9bb9 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.java @@ -12,6 +12,7 @@ package com.facebook.react.views.scroll; import android.view.View; import android.view.ViewGroup; +import com.facebook.react.bridge.JSApplicationIllegalArgumentException; import com.facebook.react.bridge.ReactContext; import com.facebook.react.uimanager.UIManagerModule; @@ -21,6 +22,9 @@ import com.facebook.react.uimanager.UIManagerModule; public class ReactScrollViewHelper { public static final long MOMENTUM_DELAY = 20; + public static final String OVER_SCROLL_ALWAYS = "always"; + public static final String AUTO = "auto"; + public static final String OVER_SCROLL_NEVER = "never"; /** * Shared by {@link ReactScrollView} and {@link ReactHorizontalScrollView}. @@ -64,4 +68,16 @@ public class ReactScrollViewHelper { scrollView.getWidth(), scrollView.getHeight())); } + + public static int parseOverScrollMode(String jsOverScrollMode) { + if (jsOverScrollMode == null || jsOverScrollMode.equals(AUTO)) { + return View.OVER_SCROLL_IF_CONTENT_SCROLLS; + } else if (jsOverScrollMode.equals(OVER_SCROLL_ALWAYS)) { + return View.OVER_SCROLL_ALWAYS; + } else if (jsOverScrollMode.equals(OVER_SCROLL_NEVER)) { + return View.OVER_SCROLL_NEVER; + } else { + throw new JSApplicationIllegalArgumentException("wrong overScrollMode: " + jsOverScrollMode); + } + } } 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 76cc41ac9..c70f8cebc 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 @@ -14,6 +14,7 @@ import javax.annotation.Nullable; import java.util.Map; import android.graphics.Color; +import android.view.View; import com.facebook.react.bridge.ReadableArray; import com.facebook.react.common.MapBuilder; @@ -107,6 +108,14 @@ public class ReactScrollViewManager view.setEndFillColor(color); } + /** + * Controls overScroll behaviour + */ + @ReactProp(name = "overScrollMode") + public void setOverScrollMode(ReactScrollView view, String value) { + view.setOverScrollMode(ReactScrollViewHelper.parseOverScrollMode(value)); + } + @Override public @Nullable Map getCommandsMap() { return ReactScrollViewCommandHelper.getCommandsMap();