Skip to content

Commit

Permalink
Android: Add overScrollMode prop to ScrollView
Browse files Browse the repository at this point in the history
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 #10905

Differential Revision: D4500957

Pulled By: mkonicek

fbshipit-source-id: 873eba38183defba133c228e0c1038efa83297d3
  • Loading branch information
Adam Comella authored and facebook-github-bot committed Feb 2, 2017
1 parent a45246e commit 12c4868
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Libraries/Components/ScrollView/ScrollView.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;
Expand Down Expand Up @@ -98,6 +99,14 @@ public void setPagingEnabled(ReactHorizontalScrollView view, boolean pagingEnabl
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;

Expand All @@ -21,6 +22,9 @@
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}.
Expand Down Expand Up @@ -64,4 +68,16 @@ private static void emitScrollEvent(ViewGroup scrollView, ScrollEventType scroll
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Map;

import android.graphics.Color;
import android.view.View;

import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.common.MapBuilder;
Expand Down Expand Up @@ -107,6 +108,14 @@ public void setBottomFillColor(ReactScrollView view, int color) {
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<String, Integer> getCommandsMap() {
return ReactScrollViewCommandHelper.getCommandsMap();
Expand Down

0 comments on commit 12c4868

Please sign in to comment.