Skip to content

Commit 5a93877

Browse files
Angela HessFacebook Github Bot 4
authored andcommitted
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
1 parent a0f6704 commit 5a93877

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

Libraries/Components/ViewPager/ViewPagerAndroid.android.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ var ViewPagerAndroid = React.createClass({
123123
'none', // default
124124
'on-drag',
125125
]),
126+
127+
/**
128+
* When false, the content does not scroll.
129+
* The default value is true.
130+
*/
131+
scrollEnabled: React.PropTypes.bool,
126132
},
127133

128134
componentDidMount: function() {

ReactAndroid/src/main/java/com/facebook/react/views/viewpager/ReactViewPager.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public void onPageScrollStateChanged(int state) {
125125

126126
private final EventDispatcher mEventDispatcher;
127127
private boolean mIsCurrentItemFromJs;
128+
private boolean mScrollEnabled = true;
128129

129130
public ReactViewPager(ReactContext reactContext) {
130131
super(reactContext);
@@ -141,19 +142,31 @@ public Adapter getAdapter() {
141142

142143
@Override
143144
public boolean onInterceptTouchEvent(MotionEvent ev) {
144-
if (super.onInterceptTouchEvent(ev)) {
145+
if (mScrollEnabled && super.onInterceptTouchEvent(ev)) {
145146
NativeGestureUtil.notifyNativeGestureStarted(this, ev);
146147
return true;
147148
}
148149
return false;
149150
}
150151

152+
@Override
153+
public boolean onTouchEvent(MotionEvent ev) {
154+
if (!mScrollEnabled) {
155+
return false;
156+
}
157+
return super.onTouchEvent(ev);
158+
}
159+
151160
public void setCurrentItemFromJs(int item, boolean animated) {
152161
mIsCurrentItemFromJs = true;
153162
setCurrentItem(item, animated);
154163
mIsCurrentItemFromJs = false;
155164
}
156165

166+
public void setScrollEnabled(boolean scrollEnabled) {
167+
mScrollEnabled = scrollEnabled;
168+
}
169+
157170
/*package*/ void addViewToAdapter(View child, int index) {
158171
getAdapter().addView(child, index);
159172
}

ReactAndroid/src/main/java/com/facebook/react/views/viewpager/ReactViewPagerManager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ protected ReactViewPager createViewInstance(ThemedReactContext reactContext) {
4343
return new ReactViewPager(reactContext);
4444
}
4545

46+
47+
@ReactProp(name = "scrollEnabled", defaultBoolean = true)
48+
public void setScrollEnabled(ReactViewPager viewPager, boolean value) {
49+
viewPager.setScrollEnabled(value);
50+
}
51+
4652
@Override
4753
public boolean needsCustomLayoutForChildren() {
4854
return true;

0 commit comments

Comments
 (0)