-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDuanzi.js
236 lines (220 loc) · 6.14 KB
/
Duanzi.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import React, {Component} from 'react';
import {
Image,
FlatList,
StyleSheet,
Text,
View,
ActivityIndicator,
} from "react-native";
import moment from "moment/moment";
let pageNo = 1;//当前第几页
const REQUEST_URL = "http://m2.qiushibaike.com/article/list/text?page=";
/**
* 段子
*/
export default class Duanzi extends Component {
static navigationOptions = () => ({
title: '糗事百科',
headerStyle: {
backgroundColor: '#549cf8',
},
headerTintColor: '#fff',
});
constructor(props) {
super(props);
this.state = {
isLoading: true,
isRefreshing: false,
//网络请求状态
error: false,
errorInfo: "",
dataArray: [],
showFoot: 0, // 控制foot, 0:隐藏footer 1:已加载完成,没有更多数据 2 :显示加载中
};
}
componentDidMount() {
this.fetchData(pageNo);
}
fetchData(pageNo) {
fetch(REQUEST_URL + pageNo)
.then((response) => {
return response.json();
})
.then((responseData) => {
let data = responseData.items;
/**
* 这里改变dataArray的值是因为防止下拉刷新数据的时候,屏幕闪烁
*/
if (this.state.isRefreshing) {
this.setState({
dataArray:[]
})
}
let foot = 0;
if (data.length === 0) {
foot = 1;
}
this.setState({
//复制数据源
dataArray: this.state.dataArray.concat(data),
showFoot: foot,
isLoading: false,
isRefreshing: false,
});
data = null;
})
.catch((error) => {
console.log(error)
});
}
render() {
if (this.state.isLoading) {
return this.renderLoadingView();
}
return (
<FlatList
data={this.state.dataArray}
renderItem={this.renderData.bind(this)}
ListFooterComponent={this._renderFooter.bind(this)}
onEndReached={this._onEndReached.bind(this)}
onRefresh={this._onRefresh.bind(this)}
refreshing={this.state.isRefreshing}
ItemSeparatorComponent={ItemDivideComponent}
onEndReachedThreshold={0.1}
keyExtractor={item => item.id}
/>
);
}
renderLoadingView() {
return (
<View style={styles.loading}>
<ActivityIndicator
animating={true}
color='#549cf8'
size="large"
/>
</View>
);
}
renderData({item}) {
return (
<View style={styles.container}>
<View>
<Image source={{uri: item.user != null ? item.user.medium : ""}} style={styles.image}/>
</View>
<View style={styles.content}>
<Text style={styles.author}>{item.user != null ? item.user.login : "无名"}</Text>
<Text style={styles.authorTime}>{moment(item.published_at * 1000).format('YYYY-MM-DD HH:mm:ss')}</Text>
<Text style={{margin: 5}}>{item.content}</Text>
</View>
</View>
);
}
_renderFooter() {
if (this.state.showFoot === 1) {
return (
<View style={styles.noMoreData}>
<Text>没有更多数据了</Text>
</View>
);
} else if (this.state.showFoot === 2) {
return (
<View style={styles.footer}>
<ActivityIndicator animating={true}
color='#549cf8'
size="small"/>
<Text>正在加载更多数据...</Text>
</View>
);
} else if (this.state.showFoot === 0) {
return (
<View style={styles.footer}>
<Text></Text>
</View>
);
}
}
_onEndReached() {
//如果是正在加载中或没有更多数据了,则返回
if (this.state.showFoot !== 0) {
return;
}
pageNo++;
this.setState({
showFoot: 2,
});
//获取数据
this.fetchData(pageNo);
}
_onRefresh() {
this.setState({
isRefreshing:true,
});
pageNo = 1;
this.fetchData(pageNo);
}
}
class ItemDivideComponent extends Component {
render() {
return (
<View style={{height: 1, backgroundColor: '#549cf8'}}/>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
},
content: {
flex: 1,
flexDirection: 'column',
},
loading: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center'
},
image: {
margin:10,
alignItems:'center',
justifyContent:'center',
width: 60,
height:60,
backgroundColor:'#549cf8',
borderColor:'green',
borderStyle:'solid',
borderRadius:50,
paddingBottom:2
},
authorTime: {
marginLeft: 8,
color: 'gray',
fontSize: 10,
},
author: {
margin: 8,
color: 'gray',
fontSize: 16,
},
footer: {
flexDirection: 'row',
height: 24,
justifyContent: 'center',
alignItems: 'center',
marginTop: 10,
marginBottom: 10,
},
noMoreData: {
flexDirection: 'row',
height: 30,
justifyContent: 'center',
alignItems: 'center',
marginTop: 10,
marginBottom: 10,
color: '#999999',
fontSize: 14
},
});