Skip to content

Commit 66c0df1

Browse files
committed
HomePage实现下拉刷新功能,超简单的方法,快来围观
1 parent 6b9268d commit 66c0df1

File tree

2 files changed

+61
-10
lines changed

2 files changed

+61
-10
lines changed

SinaWebBo/Home/HomePage.js

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
2+
/*
3+
*
4+
* 下拉刷新:因为ListView继承自ScrollView,而ScrollView有RefreshControl属性
5+
*
6+
* 没有比这个更巧合更简单的方式了
7+
*
8+
*/
19
import React, { Component } from 'react';
210
import {
311
Navigator,
@@ -8,6 +16,8 @@ import {
816
ListView,
917
AsyncStorage,
1018
ActivityIndicator,
19+
RefreshControl,
20+
1121
} from 'react-native';
1222

1323
import Const from '../Other/Const';
@@ -20,6 +30,7 @@ var resultsCache = {
2030
export default class HomePage extends Component {
2131
constructor(props) {
2232
super(props);
33+
this._panResponder = {};
2334
this.state = {
2435
isLoading: false,
2536
isLoadingTail: false,
@@ -30,13 +41,18 @@ export default class HomePage extends Component {
3041
}
3142

3243
componentDidMount() {
44+
this.setState({
45+
isLoading: true,
46+
});
3347
this.getStatuses();
3448
}
3549

3650
// 获取微博
3751
getStatuses() {
38-
{/* 有点坑爹啊,计算max_id时数组的两个length都写成了legth
39-
都没报错,默默的得到max_id=0 */}
52+
{
53+
/* 有点坑爹啊,计算max_id时数组的两个length都写成了legth
54+
都没报错,默默的得到max_id=0 */
55+
}
4056
AsyncStorage.getItem(Const.ACCESSTOKEN_KEY)
4157
.then((token) => {
4258
let max_id = resultsCache.data.length > 0 ?
@@ -65,6 +81,7 @@ export default class HomePage extends Component {
6581
})
6682
.done(() => {
6783
this.setState({
84+
isLoading: false,
6885
isLoadingTail: false,
6986
});
7087
});
@@ -73,7 +90,12 @@ export default class HomePage extends Component {
7390
.done();
7491
}
7592

76-
renderRow(status: Object,
93+
/*
94+
*
95+
* ListView事件
96+
*
97+
*/
98+
_renderRow(status: Object,
7799
sectionID: number|string,
78100
rowID: number|string,
79101
highlightRowFunc: (sectionID: ?number|string, rowID: ?number|string) => void) {
@@ -82,14 +104,14 @@ export default class HomePage extends Component {
82104
);
83105
}
84106

85-
renderFooter() {
107+
_renderFooter() {
86108
if (!this.state.isLoadingTail) {
87109
return <Text style={{alignSelf:'center'}}>----End----</Text>
88110
}
89111
return <ActivityIndicator style={styles.scrollSpinner} />
90112
}
91113

92-
renderSeparator(sectionID: number|string,
114+
_renderSeparator(sectionID: number|string,
93115
rowID: number|string,
94116
adjacentRowHighlighted: boolean) {
95117
let style = styles.rowSeparator;
@@ -101,7 +123,7 @@ export default class HomePage extends Component {
101123
);
102124
}
103125

104-
onEndReached() {
126+
_onEndReached() {
105127
if (this.state.isLoadingTail) {
106128
return;
107129
}
@@ -112,21 +134,41 @@ export default class HomePage extends Component {
112134
this.getStatuses();
113135
}
114136

137+
_onRefresh(...args) {
138+
resultsCache.data.length = 0;
139+
this.setState({
140+
isLoading: true,
141+
});
142+
this.getStatuses();
143+
}
144+
115145
render() {
116146
return (
117147
<View style={styles.container}>
118148
<ListView
119149
ref="listview"
120150
style={styles.listView}
121151
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)}
126156
automaticallyAdjustContentInsets={true}
127157
keyboardDismissMode="on-drag"
128158
keyboardShouldPersistTaps={true}
129159
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+
}
130172
/>
131173
</View>
132174
);
@@ -146,5 +188,8 @@ const styles = StyleSheet.create({
146188
},
147189
rowSeparatorHide: {
148190
opacity: 0.0,
191+
},
192+
scrollSpinner: {
193+
height: 64,
149194
}
150195
});

SinaWebBo/Release/ReleaseModal.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ export default class ReleaseModal extends Component {
6060
Alert.alert('提示', '请输入微博');
6161
}
6262

63+
/*
64+
*
65+
* 注意了,下面请求时要加上content-Type,
66+
* 不然提示“缺少必需参数status”,简直坑的一13
67+
*
68+
*/
6369
AsyncStorage.getItem(Const.ACCESSTOKEN_KEY)
6470
.then((token) => {
6571
if (token) {

0 commit comments

Comments
 (0)