近来公司的项目里面涉及到一个类似”收起-展开”的UI功能模块,先是在网上猥琐一番之后,并未发现什么.无奈之下,只能自己手撸一个了.

一开始做了个”互斥收起-展开”,公司可能不太满意,让我抄另外一个APP”非互斥收起-展开”,哎…
先上图:
非互斥缩一缩
非互斥缩一缩

互斥缩一缩
互斥缩一缩

实现原理很简单:
非互斥缩一缩: 先在json数据里面定义type标示,标示一级展开|收起状态,在UI里面就根据这个标示判断是收起还是展开,而二级收起|展开需要为每组数据定义一个唯一标示.
互斥缩一缩:先定义一个state,根据该state来显示收起UI还是展开,因为state只有一个,也就形成了只有一组是展开.同时,在判断二次展开|收起这里也需要在json里面定义一个唯一标示.
这些数据标示可以事先和后端人员商量好,让后端人员帮你弄好,也可以自己拿到数据之后再插入.
废话不多少,戴起磨砂手套开始撸!!!
直接上源码:
非互斥缩一缩

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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* eslint-disable */
import React, { Component } from 'react'
import {
StyleSheet,
View,
Image,
Text,
Dimensions,
TouchableOpacity,
FlatList,
Linking,
} from 'react-native'
const { width } = Dimensions.get('window')

export default class One extends Component {
// 构造
constructor(props) {
super(props)
// 初始状态
this.row = null
this.state = {
governmentData: [
{
id: '0',
name: '社会保障局',
type: false,
childs: [
{
index: '00',
name: '资料教室',
type: false,
tel: '5233',
message: '查询资料,注销档案',
},
{
name: '后勤部',
type: false,
index: '01',
tel: '1645682',
message: '不对外服务',
},
],
},
{
id: '1',
name: '卫生局',
type: false,
childs: [
{
name: '妇产科',
type: false,
index: '10',
tel: '112转678',
message: '接待孕妇,待产孕妇,军嫂预先',
},
{
name: '儿科',
type: false,
index: '11',
tel: '112转008',
message: '小于12周岁儿童就医',
},
{
name: '失恋科',
type: false,
index: '12',
tel: '112转出去',
message: '回家玩蛋去!!!',
},
],
},
{
id: '2',
name: '神盾局',
type: false,
childs: [
{
name: '城管大队',
type: false,
index: '20',
tel: '110',
title: '打打打',
message: '镇压起义',
},
],
},
],
isRefresh: true,
index: -1, //默认展开负一行
indexIndex: -1,
}
}

componentDidMount() {
this.flatlist = null
this.setState({
isRefresh: false,
})
}

/**
* 下拉刷新
* */
onRefresh = () => {
this.setState({
isRefresh: false,
})
}


/**
* 点击一级
* */
itemOnclick = item => {
var data = this.state.governmentData
data.map((itemm, i) => {
if (i == item.index) {
itemm.type = !itemm.type
}
})
this.setState({
governmentData: data,
})
}

/**
* 点击二级
* */
itemitemOnclick = e => {
var data = this.state.governmentData
data.map((item, index) => {
item.childs.map((item, index) => {
if (item.index == e.index) {
item.type = !item.type
}
})
})
this.setState({
governmentData: data,
})
}

/**
* 打电话
* */
tellPhone = item => {
Linking.openURL(`tel:${item.tel}`)
}

/**
* FlatList render
* */
renderRow = item => (
<View ref={item.index}>
<View>
<TouchableOpacity onPress={() => this.itemOnclick(item)}>
<View
style={{
flexDirection: 'row',
backgroundColor: 'white',
height: 30,
alignItems: 'center',
}}
>
<View
style={{
marginLeft: 5,
width: 4,
height: 10,
paddingTop: 7,
paddingBottom: 7,
backgroundColor:'red'
}}
/>
<Text style={{ color: '#333333', marginLeft: 5, fontSize: 16 }}>
{item.item.name}
</Text>
{/* 右边箭头 */}
<Image
source={
item.item.type
? require('./image/btnDown.png')
: require('./image/right.png')
}
style={{ position: 'absolute', right: 15, width: 15, height: 15 }}
/>
</View>
</TouchableOpacity>
<View
style={{
backgroundColor: 'rgb(228,228,228)',
width: width,
height: 1,
}}
/>
</View>
{item.item.type
? item.item.childs.map((item, index) => (
<View style={{ backgroundColor: 'white' }} key={index}>
<View style={{ marginLeft: 7, marginTop: 10 }}>
<View style={{ flexDirection: 'row' }}>
<Image
source={
item.type
? require('./image/btnDownB.png')
: require('./image/btnRightB.png')
}
style={{ width: 10, height: 10 }}
/>
<Text
onPress={() => this.itemitemOnclick(item)}
style={{ marginLeft: 7 }}
>
{item.name}
</Text>
</View>
<View
style={{
backgroundColor: 'rgb(228,228,228)',
width: width,
height: 1,
marginTop: 10,
}}
/>
</View>
{item.type ? (
<View>
<View
style={{
height: 40,
alignItems: 'center',
flexDirection: 'row',
}}
>
<TouchableOpacity
style={{ flexDirection: 'row' }}
onPress={() => this.tellPhone(item)}
>
<Image
source={require('./image/phone.png')}
style={{ marginLeft: 10, width: 15, height: 15 }}
/>
<Text style={{ marginLeft: 10 }}>电话 {item.tel}</Text>
</TouchableOpacity>
</View>
<View
style={{
backgroundColor: 'rgb(228,228,228)',
width: width,
height: 1,
}}
/>

<View
style={{
height: 40,
alignItems: 'center',
flexDirection: 'row',
}}
>
<Image
source={require('./image/tlak.png')}
style={{ marginLeft: 10, width: 15, height: 15 }}
/>
<Text style={{ marginLeft: 10 }}>留言 {item.message}</Text>
</View>
<View
style={{
backgroundColor: 'rgb(228,228,228)',
width: width,
height: 1,
}}
/>
</View>
) : null}
</View>
))
: null}
</View>
)

render() {
return (
<FlatList
data={this.state.governmentData}
renderItem={this.renderRow}
onRefresh={() => this.onRefresh()}
refreshing={this.state.isRefresh}
/>
)
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
},
icon: {
width: 22,
height: 22,
},
})


互斥缩一缩

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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* eslint-disable */
import React, { Component } from 'react'
import {
StyleSheet,
View,
Image,
Text,
Dimensions,
TouchableOpacity,
FlatList,
Linking,
} from 'react-native'
const { width } = Dimensions.get('window')

export default class Two extends Component {
// 构造
constructor(props) {
super(props)
// 初始状态
this.state = {
governmentData: [
{
name: '社会保障局',
childs: [
{
index:0,
name: '资料教室',
tel: '5233',
message: '查询资料,注销档案',
},
{
index:1,
name: '后勤部',
tel: '1645682',
message: '不对外服务',
},
],
},
{
name: '卫生局',
childs: [
{
index:0,
name: '妇产科',
tel: '112转678',
message: '接待孕妇,待产孕妇,军嫂预先',
},
{
index:1,
name: '儿科',
tel: '112转008',
message: '小于12周岁儿童就医',
},
{
index:2,
name: '失恋科',
tel: '112转出去',
message: '回家玩蛋去!!!',
},
],
},
{
name: '神盾局',
childs: [
{
index:0,
name: '城管大队',
tel: '110',
title: '打打打',
message: '镇压起义',
},
],
},
],
isRefresh: true,
index: -1, //默认展开负一行
indexIndex: -1,
}
}

componentDidMount() {
this.setState({
isRefresh: false,
})
}

/**
* 下拉刷新
* */
onRefresh = () => {
this.setState({
isRefresh: false,
})
}

/**
* 点击一级
* */
itemOnclick = item => {
this.setState({
index: item.index,
indexIndex: -1
})
}

/**
* 点击二级
* */
itemitemOnclick = e => {
this.setState({
indexIndex: e.index,
})
}

/**
* 打电话
* */
tellPhone = item => {
Linking.openURL(`tel:${item.tel}`)
}

/**
* FlatList render
* */
renderRow = item => (
<View ref={item.index}>
<View>
<TouchableOpacity onPress={() => this.itemOnclick(item)}>
<View
style={{
flexDirection: 'row',
backgroundColor: 'white',
height: 30,
alignItems: 'center',
}}
>
<View
style={{
marginLeft: 5,
width: 4,
height: 10,
paddingTop: 7,
paddingBottom: 7,
backgroundColor:'red'
}}
/>
<Text style={{ color: '#333333', marginLeft: 5, fontSize: 16 }}>
{item.item.name}
</Text>
{/* 右边箭头 */}
<Image
source={
item.index == this.state.index
? require('./image/btnDown.png')
: require('./image/right.png')
}
style={{ position: 'absolute', right: 15, width: 15, height: 15 }}
/>
</View>
</TouchableOpacity>
<View
style={{
backgroundColor: 'rgb(228,228,228)',
width: width,
height: 1,
}}
/>
</View>
{item.index == this.state.index
? item.item.childs.map((item, index) => (
<View style={{ backgroundColor: 'white' }} key={index}>
<View style={{ marginLeft: 7, marginTop: 10 }}>
<View style={{ flexDirection: 'row' }}>
<Image
source={
index == this.state.indexIndex
? require('./image/btnDownB.png')
: require('./image/btnRightB.png')
}
style={{ width: 10, height: 10 }}
/>
<Text
onPress={() => this.itemitemOnclick(item)}
style={{ marginLeft: 7 }}
>
{item.name}
</Text>
</View>
<View
style={{
backgroundColor: 'rgb(228,228,228)',
width: width,
height: 1,
marginTop: 10,
}}
/>
</View>
{item.index == this.state.indexIndex
? (
<View>
<View
style={{
height: 40,
alignItems: 'center',
flexDirection: 'row',
}}
>
<TouchableOpacity
style={{ flexDirection: 'row' }}
onPress={() => this.tellPhone(item)}
>
<Image
source={require('./image/phone.png')}
style={{ marginLeft: 10, width: 15, height: 15 }}
/>
<Text style={{ marginLeft: 10 }}>电话 {item.tel}</Text>
</TouchableOpacity>
</View>
<View
style={{
backgroundColor: 'rgb(228,228,228)',
width: width,
height: 1,
}}
/>

<View
style={{
height: 40,
alignItems: 'center',
flexDirection: 'row',
}}
>
<Image
source={require('./image/tlak.png')}
style={{ marginLeft: 10, width: 15, height: 15 }}
/>
<Text style={{ marginLeft: 10 }}>留言 {item.message}</Text>
</View>
<View
style={{
backgroundColor: 'rgb(228,228,228)',
width: width,
height: 1,
}}
/>
</View>
) : null}
</View>
))
: null}
</View>
)

render() {
return (
<FlatList
data={this.state.governmentData}
renderItem={this.renderRow}
onRefresh={() => this.onRefresh()}
refreshing={this.state.isRefresh}
/>
)
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
},
icon: {
width: 22,
height: 22,
},
})


源码