@@ -7,31 +7,56 @@ import React, {
77export default class InfiniteScrollView extends Component {
88 constructor ( props ) {
99 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+
1018 this . _scrollView = null ;
1119 this . _offscreenPages = this . props . offScreenPages || 1 ;
20+ this . _renderedRange = { } ;
1221 this . state = {
13- index : 0 ,
22+ index : index ,
23+ fromIndex : fromIndex ,
24+ toIndex : toIndex ,
1425 size : {
1526 width : 0 ,
1627 height : 0 ,
1728 }
1829 }
1930 }
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+ }
2044 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 (
2651 < View style = { this . props . style } onLayout = { ( event ) => this . _layoutChanged ( event ) } >
2752 < ScrollView
53+ style = { { overflow : 'visible' } }
2854 ref = { ( scrollView ) => { this . _scrollView = scrollView ; } }
2955 horizontal = { this . props . horizontal }
3056 automaticallyAdjustContentInsets = { false }
3157 showsHorizontalScrollIndicator = { false }
3258 showsVerticalScrollIndicator = { false }
3359 pagingEnabled = { true }
34- contentOffset = { contentOffset }
3560 onMomentumScrollEnd = { ( e ) => this . _scrollEnded ( e ) } >
3661 { pages }
3762 </ ScrollView >
@@ -46,14 +71,21 @@ export default class InfiniteScrollView extends Component {
4671 width : width ,
4772 height : height
4873 }
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 ;
5082 }
51- _createPages ( ) {
83+ _createPages ( range ) {
5284 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 ) ) ;
5687 }
88+ this . _renderedRange = range ;
5789 return pages ;
5890 }
5991 _renderPage ( index ) {
@@ -65,15 +97,7 @@ export default class InfiniteScrollView extends Component {
6597 }
6698 _scrollEnded ( event ) {
6799 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 ) ;
77101 this . setState ( { index : index } ) ;
78102 }
79103}
0 commit comments