1
+
2
+ /*
3
+ *
4
+ * 下拉刷新:因为ListView继承自ScrollView,而ScrollView有RefreshControl属性
5
+ *
6
+ * 没有比这个更巧合更简单的方式了
7
+ *
8
+ */
1
9
import React , { Component } from 'react' ;
2
10
import {
3
11
Navigator ,
@@ -8,6 +16,8 @@ import {
8
16
ListView ,
9
17
AsyncStorage ,
10
18
ActivityIndicator ,
19
+ RefreshControl ,
20
+
11
21
} from 'react-native' ;
12
22
13
23
import Const from '../Other/Const' ;
@@ -20,6 +30,7 @@ var resultsCache = {
20
30
export default class HomePage extends Component {
21
31
constructor ( props ) {
22
32
super ( props ) ;
33
+ this . _panResponder = { } ;
23
34
this . state = {
24
35
isLoading : false ,
25
36
isLoadingTail : false ,
@@ -30,13 +41,18 @@ export default class HomePage extends Component {
30
41
}
31
42
32
43
componentDidMount ( ) {
44
+ this . setState ( {
45
+ isLoading : true ,
46
+ } ) ;
33
47
this . getStatuses ( ) ;
34
48
}
35
49
36
50
// 获取微博
37
51
getStatuses ( ) {
38
- { /* 有点坑爹啊,计算max_id时数组的两个length都写成了legth
39
- 都没报错,默默的得到max_id=0 */ }
52
+ {
53
+ /* 有点坑爹啊,计算max_id时数组的两个length都写成了legth
54
+ 都没报错,默默的得到max_id=0 */
55
+ }
40
56
AsyncStorage . getItem ( Const . ACCESSTOKEN_KEY )
41
57
. then ( ( token ) => {
42
58
let max_id = resultsCache . data . length > 0 ?
@@ -65,6 +81,7 @@ export default class HomePage extends Component {
65
81
} )
66
82
. done ( ( ) => {
67
83
this . setState ( {
84
+ isLoading : false ,
68
85
isLoadingTail : false ,
69
86
} ) ;
70
87
} ) ;
@@ -73,7 +90,12 @@ export default class HomePage extends Component {
73
90
. done ( ) ;
74
91
}
75
92
76
- renderRow ( status : Object ,
93
+ /*
94
+ *
95
+ * ListView事件
96
+ *
97
+ */
98
+ _renderRow ( status : Object ,
77
99
sectionID : number | string ,
78
100
rowID : number | string ,
79
101
highlightRowFunc : ( sectionID : ?number | string , rowID : ?number | string ) = > void ) {
@@ -82,14 +104,14 @@ export default class HomePage extends Component {
82
104
) ;
83
105
}
84
106
85
- renderFooter ( ) {
107
+ _renderFooter ( ) {
86
108
if ( ! this . state . isLoadingTail ) {
87
109
return < Text style = { { alignSelf :'center' } } > ----End----</ Text >
88
110
}
89
111
return < ActivityIndicator style = { styles . scrollSpinner } />
90
112
}
91
113
92
- renderSeparator ( sectionID : number | string ,
114
+ _renderSeparator ( sectionID : number | string ,
93
115
rowID : number | string ,
94
116
adjacentRowHighlighted : boolean ) {
95
117
let style = styles . rowSeparator ;
@@ -101,7 +123,7 @@ export default class HomePage extends Component {
101
123
) ;
102
124
}
103
125
104
- onEndReached ( ) {
126
+ _onEndReached ( ) {
105
127
if ( this . state . isLoadingTail ) {
106
128
return ;
107
129
}
@@ -112,21 +134,41 @@ export default class HomePage extends Component {
112
134
this . getStatuses ( ) ;
113
135
}
114
136
137
+ _onRefresh ( ...args ) {
138
+ resultsCache . data . length = 0 ;
139
+ this . setState ( {
140
+ isLoading : true ,
141
+ } ) ;
142
+ this . getStatuses ( ) ;
143
+ }
144
+
115
145
render ( ) {
116
146
return (
117
147
< View style = { styles . container } >
118
148
< ListView
119
149
ref = "listview"
120
150
style = { styles . listView }
121
151
dataSource = { this . state . dataSource }
122
- renderRow = { this . renderRow . bind ( this ) }
123
- renderFooter = { this . renderFooter . bind ( this ) }
124
- renderSeparator = { this . renderSeparator . bind ( this ) }
125
- onEndReached = { this . onEndReached . bind ( this ) }
152
+ renderRow = { this . _renderRow . bind ( this ) }
153
+ renderFooter = { this . _renderFooter . bind ( this ) }
154
+ renderSeparator = { this . _renderSeparator . bind ( this ) }
155
+ onEndReached = { this . _onEndReached . bind ( this ) }
126
156
automaticallyAdjustContentInsets = { true }
127
157
keyboardDismissMode = "on-drag"
128
158
keyboardShouldPersistTaps = { true }
129
159
showsVerticalScrollIndicator = { true }
160
+
161
+ refreshControl = {
162
+ < RefreshControl
163
+ refreshing = { this . state . isLoading }
164
+ onRefresh = { this . _onRefresh . bind ( this ) }
165
+ tintColor = "#ff0000"
166
+ title = "Loading..."
167
+ titleColor = "#00ff00"
168
+ colors = { [ '#ff0000' , '#00ff00' , '#0000ff' ] }
169
+ progressBackgroundColor = "#ffff00"
170
+ />
171
+ }
130
172
/>
131
173
</ View >
132
174
) ;
@@ -146,5 +188,8 @@ const styles = StyleSheet.create({
146
188
} ,
147
189
rowSeparatorHide : {
148
190
opacity : 0.0 ,
191
+ } ,
192
+ scrollSpinner : {
193
+ height : 64 ,
149
194
}
150
195
} ) ;
0 commit comments