@@ -56,10 +56,12 @@ const ScrollableTabView = React.createClass({
56
56
} ,
57
57
58
58
getInitialState ( ) {
59
+ const width = Dimensions . get ( 'window' ) . width ;
59
60
return {
60
61
currentPage : this . props . initialPage ,
62
+ scrollX : new Animated . Value ( this . props . initialPage * width ) ,
61
63
scrollValue : new Animated . Value ( this . props . initialPage ) ,
62
- containerWidth : Dimensions . get ( 'window' ) . width ,
64
+ containerWidth : width ,
63
65
sceneKeys : this . newSceneKeys ( { currentPage : this . props . initialPage , } ) ,
64
66
} ;
65
67
} ,
@@ -74,6 +76,12 @@ const ScrollableTabView = React.createClass({
74
76
this . setTimeout ( ( ) => {
75
77
InteractionManager . runAfterInteractions ( scrollFn ) ;
76
78
} , 0 ) ;
79
+
80
+ this . state . scrollX . addListener ( ( { value, } ) => {
81
+ const scrollValue = value / this . state . containerWidth ;
82
+ this . state . scrollValue . setValue ( scrollValue ) ;
83
+ this . props . onScroll ( scrollValue ) ;
84
+ } ) ;
77
85
} ,
78
86
79
87
componentWillReceiveProps ( props ) {
@@ -142,16 +150,19 @@ const ScrollableTabView = React.createClass({
142
150
143
151
renderScrollableContent ( ) {
144
152
const scenes = this . _composeScenes ( ) ;
145
- return < ScrollView
153
+ return < Animated . ScrollView
146
154
horizontal
147
155
pagingEnabled
148
156
automaticallyAdjustContentInsets = { false }
149
157
contentOffset = { { x : this . props . initialPage * this . state . containerWidth , } }
150
158
ref = { ( scrollView ) => { this . scrollView = scrollView ; } }
151
- onScroll = { ( e ) => {
152
- const offsetX = e . nativeEvent . contentOffset . x ;
153
- this . _updateScrollValue ( offsetX / this . state . containerWidth ) ;
154
- } }
159
+ onScroll = {
160
+ Animated . event ( [ {
161
+ nativeEvent : { contentOffset : { x : this . state . scrollX } }
162
+ } ] , {
163
+ useNativeDriver : true ,
164
+ } )
165
+ }
155
166
onMomentumScrollBegin = { this . _onMomentumScrollBeginAndEnd }
156
167
onMomentumScrollEnd = { this . _onMomentumScrollBeginAndEnd }
157
168
scrollEventThrottle = { 16 }
@@ -164,7 +175,7 @@ const ScrollableTabView = React.createClass({
164
175
{ ...this . props . contentProps }
165
176
>
166
177
{ scenes }
167
- </ ScrollView > ;
178
+ </ Animated . ScrollView > ;
168
179
} ,
169
180
170
181
_composeScenes ( ) {
@@ -209,11 +220,6 @@ const ScrollableTabView = React.createClass({
209
220
} ) ;
210
221
} ,
211
222
212
- _updateScrollValue ( value ) {
213
- this . state . scrollValue . setValue ( value ) ;
214
- this . props . onScroll ( value ) ;
215
- } ,
216
-
217
223
_handleLayout ( e ) {
218
224
const { width, } = e . nativeEvent . layout ;
219
225
@@ -235,6 +241,7 @@ const ScrollableTabView = React.createClass({
235
241
goToPage : this . goToPage ,
236
242
tabs : this . _children ( ) . map ( ( child ) => child . props . tabLabel ) ,
237
243
activeTab : this . state . currentPage ,
244
+ scrollX : this . state . scrollX ,
238
245
scrollValue : this . state . scrollValue ,
239
246
containerWidth : this . state . containerWidth ,
240
247
} ;
0 commit comments