React Native使用FlatList组件实现上拉加载
程序员文章站
2022-07-02 21:46:45
...
运行成功截图:
代码如下:
import React, { Component } from 'react';
import {
View,
StyleSheet,
FlatList,
Text,
RefreshControl,
ActivityIndicator,
} from 'react-native';
import { thisExpression } from '@babel/types';
const tempDatas = [
{
color:'blue',
title:'常温',
value:70,
},
{
color:'blue',
title:'常温',
value:70,
},
{
color:'pink',
title:'高温',
value:70,
},
{
color:'blue',
title:'常温',
value:70,
},
{
color:'blue',
title:'常温',
value:70,
},
{
color:'blue',
title:'常温',
value:70,
},
{
color:'blue',
title:'常温',
value:70,
},
{
color:'gray',
title:'低温',
value:70,
},
{
color:'blue',
title:'常温',
value:70,
},
{
color:'blue',
title:'常温',
value:70,
},
{
color:'blue',
title:'常温',
value:70,
},
{
color:'blue',
title:'常温',
value:70,
},
{
color:'blue',
title:'常温',
value:70,
},
];
const tempAddDatas = [];
for(var i=10;i<12;i++){
tempAddDatas.push({
color:'blue',
title:'新增'+i+'',
value:66,
})
}
export default class ListDemo extends Component {
constructor() {
super();
this.state = {
isLoading : false,
dataArray : tempDatas,
}
}
loadData(pullUp) {
setTimeout( ()=> {
var dataArray = [];
if(pullUp) {
dataArray = this.state.dataArray;
dataArray = [...this.state.dataArray,...tempAddDatas]//每次新增
}
this.setState({
dataArray : dataArray,
isLoading : false,
})
}, 1000);
}
separator = () =>{
return <View style={{height:1,backgroundColor:'#999999'}}/>;
}
_renderItem(data) {
return (
<View style = { styles.item}>
<Text style = { styles.itemTextFrist }
style={{backgroundColor:data.item.color,
height:15,
width:15,
marginRight:10,
borderBottomLeftRadius:10,
borderBottomRightRadius:10,
borderTopLeftRadius:10,
borderTopRightRadius:10,
}}>
</Text>
<Text style = { styles.itemText } >{ data.item.title }:</Text>
<Text style = { styles.itemText }>{ data.item.value }</Text>
</View>
);
}
genIndicator () {
return <View style = { styles.IndicatorContainer}>
<ActivityIndicator //显示一个圆形的 loading 提示符号。
style = { styles.Indicator }
size = {"small"}
animating = {true}
color = { 'blue' }
/>
<Text>正在加载更多</Text>
</View>
}
render () {
return (
<View style = { styles.container }>
<Text style = {styles.headbox}>sd</Text>
<FlatList
data = { this.state.dataArray }
renderItem = { (data) => this._renderItem(data) }
keyExtractor={(item,index) => index}
numColumns ={1}//一行一列
ListFooterComponent = { () => this.genIndicator()}//底部组件
onEndReached = {//列表被滚动到距离内容最底部不足onEndReachedThreshold设置的的距离时调用。
() => {
this.loadData(true);
}
}
onEndReachedThreshold={40}//当距离内容与最底部40时触发onEndReached回调
ItemSeparatorComponent={this.separator}//分割线
/>
</View>
);
}
}
const styles = StyleSheet.create({
container : {
flex : 1,
},
headbox : {
height : 40,
lineHeight:40,
paddingLeft : 15,
backgroundColor : 'blue',
color:'white',
fontSize : 20,
},
item : {
height : 40,
paddingLeft : 15,
alignItems : "center",
flexDirection:'row',
width:90,
justifyContent:'space-between'
},
itemText : {
backgroundColor : "white",
fontSize : 20,
},
IndicatorContainer : {
alignItems : "center"
},
Indicator : {
marginBottom : 10,
},
});
上一篇: SSM框架整合搭建(七)
推荐阅读
-
vue瀑布流组件实现上拉加载更多
-
React-Native实现ListView组件之上拉刷新实例(iOS和Android通用)
-
react-native ListView下拉刷新上拉加载实现代码
-
vue使用better-scroll实现下拉刷新、上拉加载
-
解决Vue使用mint-ui loadmore实现上拉加载与下拉刷新出现一个页面使用多个上拉加载后冲突问题
-
React Native 自定义下拉刷新上拉加载的列表的示例
-
jquery使用iscorll实现上拉、下拉加载刷新
-
react native基于FlatList下拉刷新上拉加载实现代码示例
-
React Native使用FlatList组件实现上拉加载
-
react-native flatlist 上拉加载onEndReached方法频繁触发的问题