@@ -10,10 +10,12 @@ var {
1010 StyleSheet,
1111 ViewPagerAndroid,
1212 PropTypes,
13+ InteractionManager,
1314} = React ;
1415
1516var DefaultTabBar = require ( './DefaultTabBar' ) ;
1617var deviceWidth = Dimensions . get ( 'window' ) . width ;
18+ var deviceHeight = Dimensions . get ( 'window' ) . height ;
1719
1820var ScrollableTabView = React . createClass ( {
1921 statics : {
@@ -39,6 +41,10 @@ var ScrollableTabView = React.createClass({
3941 return {
4042 currentPage : this . props . initialPage ,
4143 scrollValue : new Animated . Value ( this . props . initialPage ) ,
44+ container : {
45+ width : deviceWidth ,
46+ height : deviceHeight ,
47+ }
4248 } ;
4349 } ,
4450
@@ -52,7 +58,7 @@ var ScrollableTabView = React.createClass({
5258 this . props . onChangeTab ( { i : pageNumber , ref : this . props . children [ pageNumber ] } ) ;
5359
5460 if ( Platform . OS === 'ios' ) {
55- var offset = pageNumber * deviceWidth
61+ var offset = pageNumber * this . state . container . width ;
5662 this . scrollView . scrollTo ( 0 , offset ) ;
5763 } else {
5864 this . scrollView . setPage ( pageNumber ) ;
@@ -78,26 +84,32 @@ var ScrollableTabView = React.createClass({
7884 horizontal
7985 pagingEnabled
8086 style = { styles . scrollableContentIOS }
81- contentOffset = { { x :this . props . initialPage * deviceWidth } }
87+ contentOffset = { { x :this . props . initialPage * this . state . container . width } }
8288 ref = { ( scrollView ) => { this . scrollView = scrollView } }
8389 onScroll = { ( e ) => {
8490 var offsetX = e . nativeEvent . contentOffset . x ;
85- this . _updateScrollValue ( offsetX / deviceWidth ) ;
91+ this . _updateScrollValue ( offsetX / this . state . container . width ) ;
8692 } }
8793 onMomentumScrollBegin = { ( e ) => {
8894 var offsetX = e . nativeEvent . contentOffset . x ;
89- this . _updateSelectedPage ( parseInt ( offsetX / deviceWidth ) ) ;
95+ this . _updateSelectedPage ( parseInt ( offsetX / this . state . container . width ) ) ;
9096 } }
9197 onMomentumScrollEnd = { ( e ) => {
9298 var offsetX = e . nativeEvent . contentOffset . x ;
93- this . _updateSelectedPage ( parseInt ( offsetX / deviceWidth ) ) ;
99+ this . _updateSelectedPage ( parseInt ( offsetX / this . state . container . width ) ) ;
94100 } }
95101 scrollEventThrottle = { 16 }
96102 showsHorizontalScrollIndicator = { false }
97103 scrollEnabled = { ! this . props . locked }
98104 directionalLockEnabled
99105 alwaysBounceVertical = { false } >
100- { this . props . children }
106+ { this . props . children . map ( ( child , idx ) => {
107+ return < View
108+ key = { child . props . tabLabel + '_' + idx }
109+ style = { { width : this . state . container . width } } >
110+ { child }
111+ </ View >
112+ } ) }
101113 </ ScrollView >
102114 ) ;
103115 } else {
@@ -113,10 +125,10 @@ var ScrollableTabView = React.createClass({
113125 ref = { ( scrollView ) => { this . scrollView = scrollView } } >
114126 { this . props . children . map ( ( child , idx ) => {
115127 return < View
116- key = { child . props . tabLabel + '_' + idx }
117- style = { { width : deviceWidth } } >
118- { child }
119- </ View >
128+ key = { child . props . tabLabel + '_' + idx }
129+ style = { { width : this . state . container . width } } >
130+ { child }
131+ </ View >
120132 } ) }
121133 </ ViewPagerAndroid >
122134 ) ;
@@ -136,11 +148,19 @@ var ScrollableTabView = React.createClass({
136148 this . state . scrollValue . setValue ( value ) ;
137149 } ,
138150
139- render ( ) {
140- var translateX = this . state . scrollValue . interpolate ( {
141- inputRange : [ 0 , 1 ] , outputRange : [ 0 , - deviceWidth ]
142- } ) ;
151+ _handleLayout ( e ) {
152+ var { width, height} = e . nativeEvent . layout ;
153+ var container = this . state . container ;
154+
155+ if ( width !== container . width || height !== container . height ) {
156+ this . setState ( { container : e . nativeEvent . layout } ) ;
157+ InteractionManager . runAfterInteractions ( ( ) => {
158+ this . goToPage ( this . state . currentPage ) ;
159+ } ) ;
160+ }
161+ } ,
143162
163+ render ( ) {
144164 var tabBarProps = {
145165 goToPage : this . goToPage ,
146166 tabs : this . props . children . map ( ( child ) => child . props . tabLabel ) ,
@@ -150,10 +170,11 @@ var ScrollableTabView = React.createClass({
150170 backgroundColor : this . props . tabBarBackgroundColor ,
151171 activeTextColor : this . props . tabBarActiveTextColor ,
152172 inactiveTextColor : this . props . tabBarInactiveTextColor ,
173+ containerWidth : this . state . container . width ,
153174 } ;
154175
155176 return (
156- < View style = { styles . container } >
177+ < View style = { styles . container } onLayout = { this . _handleLayout } >
157178 { this . props . tabBarPosition === 'top' ? this . renderTabBar ( tabBarProps ) : null }
158179 { this . renderScrollableContent ( ) }
159180 { this . props . tabBarPosition === 'bottom' ? this . renderTabBar ( tabBarProps ) : null }
0 commit comments