-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRefreshableListView.js
188 lines (167 loc) · 4.48 KB
/
RefreshableListView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
var React = require('react-native')
var {
ListView,
StyleSheet,
View,
Text,
Dimensions,
PropTypes,
RefreshControl
} = React;
var TopButton = require('./TopButton'),
Loader = require('react-native-sk-loader'), // 加载器
{width} = Dimensions.get('window');
var RefreshableListView = React.createClass({
statics: {
DataSource: ListView.DataSource,
},
propTypes: {
onRefresh: PropTypes.func.isRequired, // 刷新数据的回调
onLoadMore: PropTypes.func.isRequired, // 加载更多数据的回调
},
listHeight: 0, // listview(scrollview)高度
contentHeight: 0, // contentView高度
getDefaultProps() {
return {
showLoadMore: false, // 是否显示加载更多
hasTopButton: false, // 是否显示回到顶部的按钮
}
},
getInitialState() {
return {
isRefreshing: false, // 是否正在刷新
isLoadingMore: false, // 是否正在加载更多
}
},
// 渲染脚部
renderFooter: function() {
// 没有更多/下一页数据
if (!this.props.showLoadMore) {
return null;
}
// 自定义渲染
if (this.props.renderFooter) {
return this.props.renderFooter(this.state.isLoadingMore)
}
// 默认渲染
return (<Footer />)
},
// 划到底部的事件
handleEndReached(e) {
if (! this.props.showLoadMore) return;
this.startLoadMore();
},
// 是否正在加载
isLoading(){
return this.state.isRefreshing || this.state.isLoadingMore;
},
// 加载更多/下一页数据
startLoadMore() {
if (this.props.onLoadMore && ! this.isLoading()) {
this.state.isLoadingMore = true;
this.props.onLoadMore().then(this.finishLoadMore, this.finishLoadMore);
}
},
finishLoadMore() {
this.state.isLoadingMore = false;
},
// 刷新数据
startRefresh() {
if (this.props.onRefresh && ! this.isLoading()) {
this.setState({isRefreshing: true});
this.props.onRefresh().then(this.finishRefresh, this.finishRefresh);
}
},
finishRefresh() {
this.setState({isRefreshing: false});
},
getScrollResponder() {
return this.refs.listView.getScrollResponder();
},
setNativeProps(props) {
this.refs.listView.setNativeProps(props)
},
// 滚动顶部
scrollToTop: function(){
this.refs.listview.scrollTo({y: 0});
},
// 滚动底部
scrollToBottom: function(animated = true){
if (this.listHeight && this.contentHeight && this.contentHeight > this.listHeight) {
var scrollDistance = this.contentHeight - this.listHeight;
this.refs.listview.scrollTo({
y: scrollDistance,
animated,
});
}
},
render() {
return (
<View style={{flex:1}}>
{/* 列表页 */}
<ListView
{...this.props}
ref="listview"
style={[styles.listview, this.props.style]}
renderFooter={this.renderFooter} // 上拉加载更多的脚部
onEndReached={this.handleEndReached}
onEndReachedThreshold={0}
refreshControl={ // 下拉刷新的头部
<RefreshControl
refreshing={this.state.isRefreshing}
onRefresh={this.startRefresh}
tintColor="#ff0000"
title="Loading..."
colors={['#ff0000', '#00ff00', '#0000ff']}
progressBackgroundColor="#ffff00"
/>
}
onLayout={(event)=>{
var layout = event.nativeEvent.layout;
this.listHeight = layout.height;
}}
onContentSizeChange={(width, height)=>{
this.contentHeight = height;
}}
/>
{/* 回滚到顶部的按钮 */}
{this.props.hasTopButton && (
<TopButton onPress={this.scrollToTop} />
)}
</View>
)
},
});
// 脚部
var Footer = React.createClass({
render: function(){
var desc = '正在加载更多...';
return (
<View style={styles.loadMore} onLayout={this.props.onLayout}>
<Loader style={styles.activityIndicator} />
<Text style={styles.description}>
{desc}
</Text>
</View>
)
}
});
var styles = StyleSheet.create({
loadMore: {
// position: 'absolute',
width: width,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'transparent',
height: 60,
},
description: {
color: '#333',
marginLeft: 6,
},
activityIndicator:{
alignSelf: 'center',
},
})
module.exports = RefreshableListView;