Add ability to disable scroll on android ViewPager

Summary: Similar to ScrollView, adds ability to set scrollEnabled={false}, which prevents dragging. Paging is still possible by updating initialPage.

Reviewed By: AaaChiuuu

Differential Revision: D3209743

fb-gh-sync-id: ce4140323a03f2257a9bb310c7285418b01abae7
fbshipit-source-id: ce4140323a03f2257a9bb310c7285418b01abae7
This commit is contained in:
Angela Hess 2016-04-21 19:42:25 -07:00 committed by Facebook Github Bot 4
parent a0f6704d44
commit 5a93877673
3 changed files with 26 additions and 1 deletions

View File

@ -123,6 +123,12 @@ var ViewPagerAndroid = React.createClass({
'none', // default
'on-drag',
]),
/**
* When false, the content does not scroll.
* The default value is true.
*/
scrollEnabled: React.PropTypes.bool,
},
componentDidMount: function() {

View File

@ -125,6 +125,7 @@ import com.facebook.react.uimanager.events.NativeGestureUtil;
private final EventDispatcher mEventDispatcher;
private boolean mIsCurrentItemFromJs;
private boolean mScrollEnabled = true;
public ReactViewPager(ReactContext reactContext) {
super(reactContext);
@ -141,19 +142,31 @@ import com.facebook.react.uimanager.events.NativeGestureUtil;
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (super.onInterceptTouchEvent(ev)) {
if (mScrollEnabled && super.onInterceptTouchEvent(ev)) {
NativeGestureUtil.notifyNativeGestureStarted(this, ev);
return true;
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (!mScrollEnabled) {
return false;
}
return super.onTouchEvent(ev);
}
public void setCurrentItemFromJs(int item, boolean animated) {
mIsCurrentItemFromJs = true;
setCurrentItem(item, animated);
mIsCurrentItemFromJs = false;
}
public void setScrollEnabled(boolean scrollEnabled) {
mScrollEnabled = scrollEnabled;
}
/*package*/ void addViewToAdapter(View child, int index) {
getAdapter().addView(child, index);
}

View File

@ -43,6 +43,12 @@ public class ReactViewPagerManager extends ViewGroupManager<ReactViewPager> {
return new ReactViewPager(reactContext);
}
@ReactProp(name = "scrollEnabled", defaultBoolean = true)
public void setScrollEnabled(ReactViewPager viewPager, boolean value) {
viewPager.setScrollEnabled(value);
}
@Override
public boolean needsCustomLayoutForChildren() {
return true;