1
1
import React , { PropTypes , PureComponent } from 'react' ;
2
- import { View , ListView , ViewPropTypes } from 'react-native' ;
2
+ import { View , FlatList , ViewPropTypes } from 'react-native' ;
3
3
import Scroller from 'react-native-scroller' ;
4
4
import { createResponder } from 'react-native-gesture-responder' ;
5
5
@@ -46,8 +46,7 @@ export default class ViewPager extends PureComponent {
46
46
this . onResponderMove = this . onResponderMove . bind ( this ) ;
47
47
this . onResponderRelease = this . onResponderRelease . bind ( this ) ;
48
48
49
- const ds = new ListView . DataSource ( { rowHasChanged : ( r1 , r2 ) => r1 !== r2 } ) ;
50
- this . state = { width : 0 , height : 0 , dataSource : ds . cloneWithRows ( [ ] ) } ;
49
+ this . state = { width : 0 , height : 0 } ;
51
50
52
51
this . scroller = new Scroller ( true , ( dx , dy , scroller ) => {
53
52
if ( dx === 0 && dy === 0 && scroller . isFinished ( ) ) {
@@ -56,7 +55,7 @@ export default class ViewPager extends PureComponent {
56
55
}
57
56
} else {
58
57
const curX = this . scroller . getCurrX ( ) ;
59
- this . refs [ 'innerListView ' ] && this . refs [ 'innerListView ' ] . scrollTo ( { x : curX , animated : false } ) ;
58
+ this . refs [ 'innerFlatList ' ] && this . refs [ 'innerFlatList ' ] . scrollToOffset ( { offset : curX , animated : false } ) ;
60
59
61
60
let position = Math . floor ( curX / ( this . state . width + this . props . pageMargin ) ) ;
62
61
position = this . validPage ( position ) ;
@@ -86,13 +85,8 @@ export default class ViewPager extends PureComponent {
86
85
let { width, height} = e . nativeEvent . layout ;
87
86
let sizeChanged = this . state . width !== width || this . state . height !== height ;
88
87
if ( width && height && sizeChanged ) {
89
- // if layout changed, create a new DataSource instance to trigger renderRow
90
88
this . layoutChanged = true ;
91
- this . setState ( {
92
- width,
93
- height,
94
- dataSource : ( new ListView . DataSource ( { rowHasChanged : ( r1 , r2 ) => r1 !== r2 } ) ) . cloneWithRows ( [ ] )
95
- } ) ;
89
+ this . setState ( { width, height } ) ;
96
90
}
97
91
}
98
92
@@ -209,18 +203,22 @@ export default class ViewPager extends PureComponent {
209
203
return this . scroller . getCurrX ( ) - this . getScrollOffsetOfPage ( this . currentPage ) ;
210
204
}
211
205
212
- renderRow ( rowData , sectionID , rowID , highlightRow ) {
206
+ keyExtractor ( item , index ) {
207
+ return index ;
208
+ }
209
+
210
+ renderRow ( { item, index } ) {
213
211
const { width, height } = this . state ;
214
212
const { renderPage, pageMargin } = this . props ;
215
- let page = renderPage ( rowData , rowID ) ;
213
+ let page = renderPage ( item , index ) ;
216
214
217
215
const layout = { width : width , height : height , position : 'relative' } ;
218
216
const style = page . props . style ? [ page . props . style , layout ] : layout ;
219
217
220
218
let newProps = { ...page . props , ref : page . ref , style } ;
221
219
const element = React . createElement ( page . type , newProps ) ;
222
220
223
- if ( pageMargin > 0 && rowID > 0 ) {
221
+ if ( pageMargin > 0 && index > 0 ) {
224
222
// Do not using margin style to implement pageMargin.
225
223
// The ListView seems to calculate a wrong width for children views with margin.
226
224
return (
@@ -237,13 +235,11 @@ export default class ViewPager extends PureComponent {
237
235
const { width, height } = this . state ;
238
236
const { pageDataArray, scrollEnabled, style, removeClippedSubviews, initialListSize, scrollViewStyle } = this . props ;
239
237
240
- let dataSource = this . state . dataSource ;
241
238
if ( width && height ) {
242
239
let list = pageDataArray ;
243
240
if ( ! list ) {
244
241
list = [ ] ;
245
242
}
246
- dataSource = dataSource . cloneWithRows ( list ) ;
247
243
this . pageCount = list . length ;
248
244
}
249
245
@@ -257,16 +253,16 @@ export default class ViewPager extends PureComponent {
257
253
{ ...this . props }
258
254
style = { [ style , { flex : 1 } ] }
259
255
{ ...gestureResponder } >
260
- < ListView
256
+ < FlatList
261
257
style = { [ { flex : 1 } , scrollViewStyle ] }
262
- ref = { 'innerListView' }
258
+ ref = { 'innerFlatList' }
259
+ keyExtractor = { this . keyExtractor }
263
260
scrollEnabled = { false }
264
261
horizontal = { true }
265
- enableEmptySections = { true }
266
- dataSource = { dataSource }
267
- renderRow = { this . renderRow }
262
+ data = { pageDataArray }
263
+ renderItem = { this . renderRow }
268
264
onLayout = { this . onLayout }
269
- removeClippedSubviews = { removeClippedSubviews }
265
+ removeClippedSubviews = { removeClippedSubviews }
270
266
initialListSize = { initialListSize }
271
267
/>
272
268
</ View >
0 commit comments