-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWelfare.js
182 lines (167 loc) · 4.88 KB
/
Welfare.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
import React, {Component} from 'react';
import { Image, FlatList, StyleSheet, Text, View, ActivityIndicator,TouchableOpacity,BackHandler } from "react-native";
import BaseComponent from "./BaseComponent";
let pageNo = 1;//当前第几页
const REQUEST_URL = "http://gank.io/api/data/福利/10/";
let itemNo=0;//item的个数
/**
* 福利
*/
export default class Welfare extends BaseComponent {
static navigationOptions = {
title: "福利"
};
constructor(props) {
super(props);
this.state = {
isLoading: true,
//网络请求状态
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.results;
let dataBlob = [];
let i = itemNo;
data.map(function (item) {
dataBlob.push({
key: i,
value: item,
});
i++;
});
itemNo = i;
let foot = 0;
if (data.length === 0 || pageNo === 8) {
foot = 1;
}
this.setState({
//复制数据源
dataArray:this.state.dataArray.concat(dataBlob),
isLoading: false,
showFoot:foot,
});
data = null;
dataBlob = null;
})
.catch((error) => {
console.log(error)
});
}
render() {
if (this.state.isLoading) {
return this.renderLoadingView();
}
return (
<FlatList
data={this.state.dataArray}
renderItem={this.renderWelfare.bind(this)}
ListFooterComponent={this._renderFooter.bind(this)}
onEndReached={this._onEndReached.bind(this)}
onEndReachedThreshold={0.002}
keyExtractor={item => item.id}
horizontal={false}
numColumns = "2"
/>
);
}
renderLoadingView() {
return (
<View style={styles.container}>
<ActivityIndicator
animating={true}
color='#549cf8'
size="large"
/>
</View>
);
}
renderWelfare({ item }) {
return (
<View style={styles.container}>
<TouchableOpacity onPress={this._clickImage.bind(this, item.key)}>
<Image source={{ uri: item.value.url }} style={styles.image}/>
</TouchableOpacity>
</View>
);
}
_clickImage = (index) => {
this.props.navigation.navigate("ImagePreView", {urls: this.state.dataArray, index: index});
};
_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 />
<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 );
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
},
image: {
width: 160,
height: 180,
marginTop: 10,
borderRadius:10,
},
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
}
});