1
1
const React = require ( 'react' ) ;
2
- const { Component } = React ;
3
- const { ViewPropTypes } = ReactNative = require ( 'react-native' ) ;
2
+ const { Component, } = React ;
3
+ const { ViewPropTypes, } = ReactNative = require ( 'react-native' ) ;
4
4
const createReactClass = require ( 'create-react-class' ) ;
5
5
const PropTypes = require ( 'prop-types' ) ;
6
6
const {
7
7
Dimensions,
8
8
View,
9
9
Animated,
10
10
ScrollView,
11
- Platform,
12
11
StyleSheet,
13
- ViewPagerAndroid,
14
12
InteractionManager,
15
13
} = ReactNative ;
16
14
const TimerMixin = require ( 'react-timer-mixin' ) ;
@@ -19,9 +17,6 @@ const SceneComponent = require('./SceneComponent');
19
17
const DefaultTabBar = require ( './DefaultTabBar' ) ;
20
18
const ScrollableTabBar = require ( './ScrollableTabBar' ) ;
21
19
22
- const AnimatedViewPagerAndroid = Platform . OS === 'android' ?
23
- Animated . createAnimatedComponent ( ViewPagerAndroid ) :
24
- undefined ;
25
20
26
21
const ScrollableTabView = createReactClass ( {
27
22
mixins : [ TimerMixin , ] ,
@@ -66,35 +61,17 @@ const ScrollableTabView = createReactClass({
66
61
let positionAndroid ;
67
62
let offsetAndroid ;
68
63
69
- if ( Platform . OS === 'ios' ) {
70
- scrollXIOS = new Animated . Value ( this . props . initialPage * containerWidth ) ;
71
- const containerWidthAnimatedValue = new Animated . Value ( containerWidth ) ;
72
- // Need to call __makeNative manually to avoid a native animated bug. See
73
- // https://github.com/facebook/react-native/pull/14435
74
- containerWidthAnimatedValue . __makeNative ( ) ;
75
- scrollValue = Animated . divide ( scrollXIOS , containerWidthAnimatedValue ) ;
76
-
77
- const callListeners = this . _polyfillAnimatedValue ( scrollValue ) ;
78
- scrollXIOS . addListener (
79
- ( { value, } ) => callListeners ( value / this . state . containerWidth )
80
- ) ;
81
- } else {
82
- positionAndroid = new Animated . Value ( this . props . initialPage ) ;
83
- offsetAndroid = new Animated . Value ( 0 ) ;
84
- scrollValue = Animated . add ( positionAndroid , offsetAndroid ) ;
85
-
86
- const callListeners = this . _polyfillAnimatedValue ( scrollValue ) ;
87
- let positionAndroidValue = this . props . initialPage ;
88
- let offsetAndroidValue = 0 ;
89
- positionAndroid . addListener ( ( { value, } ) => {
90
- positionAndroidValue = value ;
91
- callListeners ( positionAndroidValue + offsetAndroidValue ) ;
92
- } ) ;
93
- offsetAndroid . addListener ( ( { value, } ) => {
94
- offsetAndroidValue = value ;
95
- callListeners ( positionAndroidValue + offsetAndroidValue ) ;
96
- } ) ;
97
- }
64
+ scrollXIOS = new Animated . Value ( this . props . initialPage * containerWidth ) ;
65
+ const containerWidthAnimatedValue = new Animated . Value ( containerWidth ) ;
66
+ // Need to call __makeNative manually to avoid a native animated bug. See
67
+ // https://github.com/facebook/react-native/pull/14435
68
+ containerWidthAnimatedValue . __makeNative ( ) ;
69
+ scrollValue = Animated . divide ( scrollXIOS , containerWidthAnimatedValue ) ;
70
+
71
+ const callListeners = this . _polyfillAnimatedValue ( scrollValue ) ;
72
+ scrollXIOS . addListener (
73
+ ( { value, } ) => callListeners ( value / this . state . containerWidth )
74
+ ) ;
98
75
99
76
return {
100
77
currentPage : this . props . initialPage ,
@@ -118,28 +95,13 @@ const ScrollableTabView = createReactClass({
118
95
} ,
119
96
120
97
componentWillUnmount ( ) {
121
- if ( Platform . OS === 'ios' ) {
122
- this . state . scrollXIOS . removeAllListeners ( ) ;
123
- } else {
124
- this . state . positionAndroid . removeAllListeners ( ) ;
125
- this . state . offsetAndroid . removeAllListeners ( ) ;
126
- }
98
+ this . state . scrollXIOS . removeAllListeners ( ) ;
127
99
} ,
128
100
129
101
goToPage ( pageNumber ) {
130
- if ( Platform . OS === 'ios' ) {
131
- const offset = pageNumber * this . state . containerWidth ;
132
- if ( this . scrollView ) {
133
- this . scrollView . getNode ( ) . scrollTo ( { x : offset , y : 0 , animated : ! this . props . scrollWithoutAnimation , } ) ;
134
- }
135
- } else {
136
- if ( this . scrollView ) {
137
- if ( this . props . scrollWithoutAnimation ) {
138
- this . scrollView . getNode ( ) . setPageWithoutAnimation ( pageNumber ) ;
139
- } else {
140
- this . scrollView . getNode ( ) . setPage ( pageNumber ) ;
141
- }
142
- }
102
+ const offset = pageNumber * this . state . containerWidth ;
103
+ if ( this . scrollView ) {
104
+ this . scrollView . getNode ( ) . scrollTo ( { x : offset , y : 0 , animated : ! this . props . scrollWithoutAnimation , } ) ;
143
105
}
144
106
145
107
const currentPage = this . state . currentPage ;
@@ -216,70 +178,42 @@ const ScrollableTabView = createReactClass({
216
178
} ,
217
179
218
180
renderScrollableContent ( ) {
219
- if ( Platform . OS === 'ios' ) {
220
- const scenes = this . _composeScenes ( ) ;
221
- return < Animated . ScrollView
222
- horizontal
223
- pagingEnabled
224
- automaticallyAdjustContentInsets = { false }
225
- contentOffset = { { x : this . props . initialPage * this . state . containerWidth , } }
226
- ref = { ( scrollView ) => { this . scrollView = scrollView ; } }
227
- onScroll = { Animated . event (
228
- [ { nativeEvent : { contentOffset : { x : this . state . scrollXIOS , } , } , } , ] ,
229
- { useNativeDriver : true , listener : this . _onScroll , }
230
- ) }
231
- onMomentumScrollBegin = { this . _onMomentumScrollBeginAndEnd }
232
- onMomentumScrollEnd = { this . _onMomentumScrollBeginAndEnd }
233
- scrollEventThrottle = { 16 }
234
- scrollsToTop = { false }
235
- showsHorizontalScrollIndicator = { false }
236
- scrollEnabled = { ! this . props . locked }
237
- directionalLockEnabled
238
- alwaysBounceVertical = { false }
239
- keyboardDismissMode = "on-drag"
240
- { ...this . props . contentProps }
241
- >
242
- { scenes }
243
- </ Animated . ScrollView > ;
244
- } else {
245
- const scenes = this . _composeScenes ( ) ;
246
- return < AnimatedViewPagerAndroid
247
- key = { this . _children ( ) . length }
248
- style = { styles . scrollableContentAndroid }
249
- initialPage = { this . props . initialPage }
250
- onPageSelected = { this . _updateSelectedPage }
251
- keyboardDismissMode = "on-drag"
252
- scrollEnabled = { ! this . props . locked }
253
- onPageScroll = { Animated . event (
254
- [ {
255
- nativeEvent : {
256
- position : this . state . positionAndroid ,
257
- offset : this . state . offsetAndroid ,
258
- } ,
259
- } , ] ,
260
- {
261
- useNativeDriver : true ,
262
- listener : this . _onScroll ,
263
- } ,
264
- ) }
265
- ref = { ( scrollView ) => { this . scrollView = scrollView ; } }
266
- { ...this . props . contentProps }
267
- >
181
+ const scenes = this . _composeScenes ( ) ;
182
+ return ( < Animated . ScrollView
183
+ horizontal
184
+ pagingEnabled
185
+ automaticallyAdjustContentInsets = { false }
186
+ contentOffset = { { x : this . props . initialPage * this . state . containerWidth , } }
187
+ ref = { ( scrollView ) => { this . scrollView = scrollView ; } }
188
+ onScroll = { Animated . event (
189
+ [ { nativeEvent : { contentOffset : { x : this . state . scrollXIOS , } , } , } , ] ,
190
+ { useNativeDriver : true , listener : this . _onScroll , }
191
+ ) }
192
+ onMomentumScrollBegin = { this . _onMomentumScrollBeginAndEnd }
193
+ onMomentumScrollEnd = { this . _onMomentumScrollBeginAndEnd }
194
+ scrollEventThrottle = { 16 }
195
+ scrollsToTop = { false }
196
+ showsHorizontalScrollIndicator = { false }
197
+ scrollEnabled = { ! this . props . locked }
198
+ directionalLockEnabled
199
+ alwaysBounceVertical = { false }
200
+ keyboardDismissMode = "on-drag"
201
+ { ...this . props . contentProps }
202
+ >
268
203
{ scenes }
269
- </ AnimatedViewPagerAndroid > ;
270
- }
204
+ </ Animated . ScrollView > ) ;
271
205
} ,
272
206
273
207
_composeScenes ( ) {
274
208
return this . _children ( ) . map ( ( child , idx ) => {
275
209
let key = this . _makeSceneKey ( child , idx ) ;
276
- return < SceneComponent
210
+ return ( < SceneComponent
277
211
key = { child . key }
278
212
shouldUpdated = { this . _shouldRenderSceneKey ( idx , this . state . currentPage ) }
279
213
style = { { width : this . state . containerWidth , } }
280
214
>
281
215
{ this . _keyExists ( this . state . sceneKeys , key ) ? child : < View tabLabel = { child . props . tabLabel } /> }
282
- </ SceneComponent > ;
216
+ </ SceneComponent > ) ;
283
217
} ) ;
284
218
} ,
285
219
@@ -313,16 +247,11 @@ const ScrollableTabView = createReactClass({
313
247
} ,
314
248
315
249
_onScroll ( e ) {
316
- if ( Platform . OS === 'ios' ) {
317
- const offsetX = e . nativeEvent . contentOffset . x ;
318
- if ( offsetX === 0 && ! this . scrollOnMountCalled ) {
319
- this . scrollOnMountCalled = true ;
320
- } else {
321
- this . props . onScroll ( offsetX / this . state . containerWidth ) ;
322
- }
250
+ const offsetX = e . nativeEvent . contentOffset . x ;
251
+ if ( offsetX === 0 && ! this . scrollOnMountCalled ) {
252
+ this . scrollOnMountCalled = true ;
323
253
} else {
324
- const { position, offset, } = e . nativeEvent ;
325
- this . props . onScroll ( position + offset ) ;
254
+ this . props . onScroll ( offsetX / this . state . containerWidth ) ;
326
255
}
327
256
} ,
328
257
@@ -332,17 +261,14 @@ const ScrollableTabView = createReactClass({
332
261
if ( ! width || width <= 0 || Math . round ( width ) === Math . round ( this . state . containerWidth ) ) {
333
262
return ;
334
263
}
335
-
336
- if ( Platform . OS === 'ios' ) {
337
- const containerWidthAnimatedValue = new Animated . Value ( width ) ;
338
- // Need to call __makeNative manually to avoid a native animated bug. See
339
- // https://github.com/facebook/react-native/pull/14435
340
- containerWidthAnimatedValue . __makeNative ( ) ;
341
- scrollValue = Animated . divide ( this . state . scrollXIOS , containerWidthAnimatedValue ) ;
342
- this . setState ( { containerWidth : width , scrollValue, } ) ;
343
- } else {
344
- this . setState ( { containerWidth : width , } ) ;
345
- }
264
+
265
+ const containerWidthAnimatedValue = new Animated . Value ( width ) ;
266
+ // Need to call __makeNative manually to avoid a native animated bug. See
267
+ // https://github.com/facebook/react-native/pull/14435
268
+ containerWidthAnimatedValue . __makeNative ( ) ;
269
+ scrollValue = Animated . divide ( this . state . scrollXIOS , containerWidthAnimatedValue ) ;
270
+ this . setState ( { containerWidth : width , scrollValue, } ) ;
271
+
346
272
this . requestAnimationFrame ( ( ) => {
347
273
this . goToPage ( this . state . currentPage ) ;
348
274
} ) ;
0 commit comments