@@ -7,31 +7,56 @@ import React, {
7
7
export default class InfiniteScrollView extends Component {
8
8
constructor ( props ) {
9
9
super ( props ) ;
10
+
11
+ var fromIndex = props . fromIndex || Number . NEGATIVE_INFINITY ;
12
+ var toIndex = props . toIndex || Number . POSITIVE_INFINITY ;
13
+
14
+ var index = 0 ;
15
+ if ( this . props . index ) index = Math . min ( Math . max ( this . props . index , fromIndex ) , toIndex ) ;
16
+ else index = Math . max ( 0 , fromIndex ) ;
17
+
10
18
this . _scrollView = null ;
11
19
this . _offscreenPages = this . props . offScreenPages || 1 ;
20
+ this . _renderedRange = { } ;
12
21
this . state = {
13
- index : 0 ,
22
+ index : index ,
23
+ fromIndex : fromIndex ,
24
+ toIndex : toIndex ,
14
25
size : {
15
26
width : 0 ,
16
27
height : 0 ,
17
28
}
18
29
}
19
30
}
31
+ shouldComponentUpdate ( nextProps , nextState ) {
32
+ var range = this . _pagesRange ( nextState ) ;
33
+ return (
34
+ nextState . size !== this . state . size ||
35
+ range . to !== this . _renderedRange . to || range . from !== this . _renderedRange . from
36
+ ) ;
37
+ }
38
+ componentDidUpdate ( ) {
39
+ var scrollTo = { animated : false }
40
+ if ( this . props . horizontal ) scrollTo . x = ( this . state . index - this . _renderedRange . from ) * this . state . size . width ;
41
+ else scrollTo . y = ( this . state . index - this . _renderedRange . from ) * this . state . size . height ;
42
+ this . _scrollView . scrollTo ( scrollTo ) ;
43
+ }
20
44
render ( ) {
21
- var pages = this . _createPages ( ) ;
22
- var contentOffset = { x :0 , y :0 } ;
23
- if ( this . props . horizontal ) contentOffset . x = this . _offscreenPages * this . state . size . width ;
24
- else contentOffset . y = this . _offscreenPages * this . state . size . height ;
25
- return (
45
+ var pages = null ;
46
+ if ( this . state . size . width > 0 && this . state . size . width > 0 ) {
47
+ var range = this . _pagesRange ( this . state ) ;
48
+ pages = this . _createPages ( range ) ;
49
+ }
50
+ return (
26
51
< View style = { this . props . style } onLayout = { ( event ) => this . _layoutChanged ( event ) } >
27
52
< ScrollView
53
+ style = { { overflow : 'visible' } }
28
54
ref = { ( scrollView ) => { this . _scrollView = scrollView ; } }
29
55
horizontal = { this . props . horizontal }
30
56
automaticallyAdjustContentInsets = { false }
31
57
showsHorizontalScrollIndicator = { false }
32
58
showsVerticalScrollIndicator = { false }
33
59
pagingEnabled = { true }
34
- contentOffset = { contentOffset }
35
60
onMomentumScrollEnd = { ( e ) => this . _scrollEnded ( e ) } >
36
61
{ pages }
37
62
</ ScrollView >
@@ -46,14 +71,21 @@ export default class InfiniteScrollView extends Component {
46
71
width : width ,
47
72
height : height
48
73
}
49
- } )
74
+ } ) ;
75
+ }
76
+ _pagesRange ( state ) {
77
+ var range = { } ;
78
+ range . from = Math . max ( state . index - this . _offscreenPages , state . fromIndex ) ;
79
+ range . to = Math . min ( range . from + 2 * this . _offscreenPages , state . toIndex ) ;
80
+ range . from = Math . min ( range . from , range . to - 2 * this . _offscreenPages ) ;
81
+ return range ;
50
82
}
51
- _createPages ( ) {
83
+ _createPages ( range ) {
52
84
var pages = [ ] ;
53
- var numberOfPages = this . _offscreenPages * 2 + 1 ;
54
- for ( i = 0 ; i < numberOfPages ; i ++ ) {
55
- pages . push ( this . _renderPage ( i + this . state . index - this . _offscreenPages ) ) ;
85
+ for ( i = range . from ; i <= range . to ; i ++ ) {
86
+ pages . push ( this . _renderPage ( i ) ) ;
56
87
}
88
+ this . _renderedRange = range ;
57
89
return pages ;
58
90
}
59
91
_renderPage ( index ) {
@@ -65,15 +97,7 @@ export default class InfiniteScrollView extends Component {
65
97
}
66
98
_scrollEnded ( event ) {
67
99
var scrollIndex = Math . round ( this . props . horizontal ? event . nativeEvent . contentOffset . x / this . state . size . width : event . nativeEvent . contentOffset . y / this . state . size . height ) ;
68
- var index = this . state . index + scrollIndex - this . _offscreenPages ;
69
- this . _positionPages ( index ) ;
70
- }
71
- _positionPages ( index ) {
72
- var scrollTo = { animated : false }
73
- if ( this . props . horizontal ) scrollTo . x = this . _offscreenPages * this . state . size . width ;
74
- else scrollTo . y = this . _offscreenPages * this . state . size . height ;
75
-
76
- this . _scrollView . scrollTo ( scrollTo ) ;
100
+ var index = this . state . index + scrollIndex - Math . min ( this . _offscreenPages , this . state . index - this . state . fromIndex ) - Math . max ( 0 , this . _offscreenPages + this . state . index - this . state . toIndex ) ;
77
101
this . setState ( { index : index } ) ;
78
102
}
79
103
}
0 commit comments