-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAreaList.js
216 lines (200 loc) · 5.28 KB
/
AreaList.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
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Image,
ListView,
ScrollView,
Text,
View,
TouchableOpacity,
Animated,
} from 'react-native';
import Icon from 'react-native-vector-icons/FontAwesome';
import Toast from 'react-native-root-toast';
var URL = 'http://vpn.jingtaomart.com/api/RegionController/getRegionList';
export default class AreaList extends Component {
//构造
constructor(props)
{
super(props);
this.state = {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => {
// console.log(row1);
// console.log(row2);
// alert(JSON.stringify(row1));
// alert(JSON.stringify(row2));
// return true;
return row1 !== row2;
},
}),
list : [],
bounceValue : new Animated.Value(0),
};
this.readRow = this.readRow.bind(this);
this.update_state = this.update_state.bind(this);
}
//数据加载完毕后执行
componentDidMount() {
this.fetchDate();
}
//基本方法, 默认执行
render() {
if(!this.state.loaded)
{
return this.dataLoading();
}
// automaticallyAdjustContentInsets={false}
// contentContainerStyle={styles.list_view}
return (
<View>
<ListView
dataSource={this.state.dataSource}
renderRow={this.readRow}
onEndReached={this.data_end}
onEndReachedThreshold={10}
/>
</View>
);
}
//获取数据
fetchDate = () => {
fetch(URL, {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
})
.then((response) => response.json())
.then((responseJson) => {
var datas = responseJson.regionAry[0].child;
this.setState({
dataSource: this.state.dataSource.cloneWithRows(datas),
list : datas,
loaded: true,
});
})
.catch((error) => {
console.error(error);
})
};
//显示加载数据中的视图
dataLoading = () => {
return (
<View style={styles.loading_view}>
<Text style={styles.loading_text}>加载数据中 ....</Text>
</View>
);
};
update_state = (city, rowID) => {
//alert(JSON.stringify(city));
let _item = Object.assign({}, city, {'isSelected': city.isSelected ? false : true});
let _newData = Object.assign({}, this.state.list, {[rowID]: _item});
// Animated.timing( // Uses easing functions
// this.state.bounceValue, // The value to drive
// {
// toValue: _item.isSelected ? 1 : 0,
// duration : 300,
// } // Configuration
// ).start(() => {
// this.setState({
// dataSource: this.state.dataSource.cloneWithRows(_newData),
// });
// });
this.setState({
dataSource: this.state.dataSource.cloneWithRows(_newData),
list : _newData,
});
};
//添加每一行数据和样式
readRow = (city, sectionID, rowID) => {
return (
<View>
<TouchableOpacity onPress={this.update_state.bind(this, city, rowID)}>
<View style={styles.city_box}>
<Image
source={{uri : city.griImg}}
style={styles.city_img}
/>
<View style={styles.city_txt_box}>
<Text style={styles.city_name}>{'name:' + city.region_name}</Text>
{city.isSelected ?
<Icon name='angle-up' size={18} color="#666" /> :
<Icon name='angle-down' size={18} color="#666" />
}
</View>
</View>
</TouchableOpacity>
<View style={styles.hideContent}>
{city.isSelected ?
<Text style={{
lineHeight : 30,
paddingBottom : 20,
}}>{city.griInfo}</Text>
: null
}
</View>
</View>
);
};
};
const styles = StyleSheet.create({
scrollView : {
flex : 1,
},
loading_view : {
flex : 1,
flexDirection : 'row',
justifyContent : 'center',
alignItems : 'center',
backgroundColor : '#BBB',
},
loading_text : {
textAlign : 'center',
color : '#666',
fontSize : 15,
fontWeight : 'bold',
letterSpacing : 2,
},
city_box : {
margin: 5,
backgroundColor: '#eee',
borderWidth: 1,
borderRadius: 5,
borderColor: '#CCC',
flexDirection: 'row',
justifyContent: 'space-between',
},
btn_box : {
flex: 1,
flexDirection: 'column',
},
city_img : {
width : 90,
height : 90,
borderTopLeftRadius: 5,
borderBottomLeftRadius: 5,
},
city_txt_box : {
height : 90,
flexDirection: 'row',
alignItems : 'center',
marginRight : 20,
},
city_name : {
fontSize : 11,
color : '#666',
marginRight : 5,
},
list_view : {
justifyContent: 'center',
flexDirection: 'row',
flexWrap: 'wrap',
},
hideContent : {
paddingLeft : 10,
paddingRight : 10,
},
});