FlatList

react-native的FlatList

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
import React, {Component} from 'react';
import {StyleSheet, Text, View,FlatList,RefreshControl,ActivityIndicator} from 'react-native';

const CITY_NAMES = ['北京','上海','广州','深圳','佛山','清远','湛江']
type Props = {};
export default class FlatListDemo extends Component<Props> {
constructor(props) {
super(props)
this.state = {
isLoading: false,
dataArray: CITY_NAMES,
}
}

loadData(refreshing) {
//如果是下拉刷新顶部loading转
if (refreshing) {
this.setState({
isLoading: true,
})
}

setTimeout( () => {
let dataArray = []
//判断是下拉刷新还是上拉下载
if (refreshing) {
for (let i = this.state.dataArray.length-1;i>=0;i--) {
dataArray.push(this.state.dataArray[i])
}
}else {
dataArray = this.state.dataArray.concat(CITY_NAMES)
}

this.setState({
dataArray:dataArray,
isLoading: false,
})
},1000)
}

_renderItem(data) {
return (
<View style={styles.item}>
<Text style={styles.text}>{data.item}</Text>
</View>
)
}

genIndicator() {
return (
<View style={styles.indicatorContainer}>
<ActivityIndicator
style={styles.indicator}
color={'red'}
size={'large'}
animating={true}
/>
<Text>正在加载更多</Text>
</View>
)
}


//onRefresh:下拉刷新
//refreshing:定义Loading状态
//RefreshControl:这一组件可以用在ScrollView或FlatList内部,为其添加下拉刷新的功能
//ListFooterComponent:尾部组件
//onEndReached:当列表被滚动到距离内容最底部不足onEndReachedThreshold的距离时调用
render() {
return (
<View style={styles.container}>
<FlatList
data={this.state.dataArray}
renderItem={(data)=>this._renderItem(data)}
// refreshing={this.state.isLoading}
// onRefresh={ () => {
// this.loadData();
// }}
refreshControl={
<RefreshControl
title={'Loading'} //loading文字
colors={'red'} //android的loading颜色
tintColor={'red'} //ios的loading颜色
titleColor={'red'} //loading文字颜色
refreshing={this.state.isLoading}
onRefresh={ () => {
this.loadData(true)
}}
/>
}
ListFooterComponent={ () => this.genIndicator()}
onEndReached={ () => {
this.loadData()
}}
/>
</View>
);
}
}