[RN] React Native 实现 FlatList上拉加载
RefreshControl实现下拉刷新功能,但官方没有提供相应的上拉加载的组件,因此在RN中实现上拉加载比下拉刷新要复杂一点。不过我们仍可以通过
FlatList的onEndReached与onEndReachedThreshold属性来实现相应效果。我们以github提供的api为例

import React, {Component} from "react";
import {ActivityIndicator, FlatList, StyleSheet, Text, View, Image} from "react-native";
const REQUEST_URL = 'https://api.github.com/search/repositories?q=javascript&sort=stars&page=';
let pageNo = ;//当前第几页
let totalPage=;//总的页数
let itemNo=;//item的个数
class Home extends Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
//网络请求状态
error: false,
errorInfo: "",
dataArray: [],
showFoot:, // 控制foot, 0:隐藏footer 1:已加载完成,没有更多数据 2 :显示加载中
isRefreshing:false,//下拉控制
}
}
//网络请求——获取第pageNo页数据
fetchData(pageNo) {
//这个是js的访问网络的方法
fetch(REQUEST_URL+pageNo)
.then((response) => response.json())
.then((responseData) => {
let data = responseData.items;
let dataBlob = [];
let i = itemNo;
data.map(function (item) {
dataBlob.push({
key: i,
value: item,
})
i++;
});
itemNo = i;
console.log("itemNo:"+itemNo);
let foot = ;
if(pageNo>=totalPage){
foot = ;//listView底部显示没有更多数据了
}
this.setState({
//复制数据源
dataArray:this.state.dataArray.concat(dataBlob),
isLoading: false,
showFoot:foot,
isRefreshing:false,
});
data = null;
dataBlob = null;
})
.catch((error) => {
this.setState({
error: true,
errorInfo: error
})
})
.done();
}
componentDidMount() {
//请求数据
this.fetchData( pageNo );
}
//加载等待页
renderLoadingView() {
return (
<View style={styles.container}>
<ActivityIndicator
animating={true}
color='red'
size="large"
/>
</View>
);
}
//加载失败view
renderErrorView() {
return (
<View style={styles.container}>
<Text>
Fail
</Text>
</View>
);
}
//返回itemView
_renderItemView({item}) {
return (
<View style={styles.itemBox}>
<View style={styles.leftArea}>
<Image style={styles.avatar} source={{uri:item.value.owner.avatar_url}} />
</View>
<View style={styles.rightArea}>
<Text style={styles.title}>{item.value.name}</Text>
<Text style={styles.desc}>{item.value.description}</Text>
</View>
</View>
);
}
renderData() {
return (
<FlatList
data={this.state.dataArray}
renderItem={this._renderItemView}
ListFooterComponent={this._renderFooter.bind(this)}
onEndReached={this._onEndReached.bind(this)}
onEndReachedThreshold={}
ItemSeparatorComponent={this._separator}
/>
);
}
render() {
//第一次加载等待的view
if (this.state.isLoading && !this.state.error) {
return this.renderLoadingView();
} else if (this.state.error) {
//请求失败view
console.log(this.state.error);
console.log(this.state.errorInfo);
return this.renderErrorView();
}
//加载数据
return this.renderData();
}
_separator(){
return <View style={{height:,backgroundColor:'#999999'}}/>;
}
_renderFooter(){
if (this.state.showFoot === ) {
return (
<View style={{height:,alignItems:'center',justifyContent:'flex-start',}}>
<Text style={{color:'#999999',fontSize:,marginTop:,marginBottom:,}}>
没有更多数据了
</Text>
</View>
);
} else if(this.state.showFoot === ) {
return (
<View style={styles.footer}>
<ActivityIndicator />
<Text>正在加载...</Text>
</View>
);
} else if(this.state.showFoot === ){
return (
<View style={styles.footer}>
<Text></Text>
</View>
);
}
}
_onEndReached(){
//如果是正在加载中或没有更多数据了,则返回
if(this.state.showFoot != ){
return ;
}
//如果当前页大于或等于总页数,那就是到最后一页了,返回
if((pageNo!=) && (pageNo>=totalPage)){
return;
} else {
pageNo++;
}
//底部显示正在加载更多数据
this.setState({showFoot:});
//获取数据
this.fetchData( pageNo );
}
}
const styles = StyleSheet.create({
container: {
flex: ,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
itemBox: {
flex:,
flexDirection:'row',
paddingTop:,
paddingBottom:,
paddingLeft:,
paddingRight:,
},
leftArea: {
flex:,
justifyContent: 'center',
alignItems: 'center',
},
avatar: {
width:,
height:,
},
rightArea: {
flex:,
justifyContent: 'center',
alignItems: 'center',
},
title: {
fontSize: ,
color: 'blue',
},
desc: {
fontSize: ,
color: 'black',
},
});
module.exports = Home;
[RN] React Native 实现 FlatList上拉加载的更多相关文章
- react-native flatlist 上拉加载onEndReached方法频繁触发的问题
问题 在写flatlist复用组件时,调用的时候如果父组件是不定高的组件,会造成组件无法显示 如果父组件样式{flex:1},则会出现下拉方法频繁触发或不正常触发的问题(我这里出现的问题是在列表第6个 ...
- reactnative中FlatList上拉加载更多的解决办法
项目app中用到了list滚动加载,把List做了下对比发现FlatList比较适合自己的项目,但是在实际运用中 onEndReached方法需要给定 onEndReachedThreshold的高度 ...
- React Native 之FlatList 下拉刷新和上拉加载更多
接上一篇代码: 只修改了FlatListDemo.js里面的代码 import React, {Fragment,Component} from 'react'; import { SafeAreaV ...
- [RN] React Native 使用 FlatList 实现九宫格布局 GridList
React Native 使用 FlatList 实现九宫格布局 先看图片演示实例: 本文以图片列表为例,实现九宫格布局! 主要有两种方法: 1)方法一: 利用FlatList的 numColumns ...
- react + iscroll5 实现完美 下拉刷新,上拉加载
经过几天的反复折腾,总算做出一个体验还不错的列表页了,主要支持了下拉刷新,上拉加载两个功能. 一开始直接采用了react-iscroll插件,它是基于iscroll插件开发的组件.但是开发过程中,发现 ...
- react-native-page-listview使用方法(自定义FlatList/ListView下拉刷新,上拉加载更多,方便的实现分页)
react-native-page-listview 对ListView/FlatList的封装,可以很方便的分页加载网络数据,还支持自定义下拉刷新View和上拉加载更多的View.兼容高版本Flat ...
- react antd上拉加载与下拉刷新与虚拟列表使用
创建项目 create-react-app antdReact 安装:antd-mobile.react-virtualized npm i antd-mobile -S npm i react-vi ...
- react-native 模仿原生 实现下拉刷新/上拉加载更多(RefreshListView)
1.下拉刷新/上拉加载更多 组件(RefreshListView) src/components/RefreshListView/index.js /** * 下拉刷新/上拉加载更多 组件(Refre ...
- react-native ListView 封装 实现 下拉刷新/上拉加载更多
1.PageListView 组件封装 src/components/PageListView/index.js /** * 上拉刷新/下拉加载更多 组件 */ import React, { Com ...
随机推荐
- SQL注入获取Sa账号密码
漏洞位置:http://168.1.1.81/Information/Search?Keyword=1111 漏洞利用: MSSQL 2000 http://168.1.1.81/Informatio ...
- 关于使用KubeSphere中的docker配置Harbor仓库http访问docker login登陆报错的解决办法
# 先进入harbor目录中,停止harbor docker-compose stop # 然后修改docker相关文件 # 第一种方式:修改/etc/docker/daemon.json { &qu ...
- 21、Cursorを使う
例: (詳しい内容が後で追加) declare @tempTB table ( PEファンドコード ) ) --1.データ格納用の変数を声明 ) --2.Cursorを声明.内容を定義 declare ...
- ELK学习笔记之logstash配置多入多出并互相隔离
0x00 概述 需求:需要利用同一logstash进程采集不同日志,输出到es的不同index,各输入输出隔离: 主要需要解决如下两个问题: 0x01 如何加载多个配置文件 普通启动方式: nohup ...
- springcolud 的学习(二).SpringCloud微服务框架
为什么选择SpringCloud因为SpringCloud出现,对微服务技术提供了非常大的帮助,因为SpringCloud 提供了一套完整的微服务解决方案,不像其他框架只是解决了微服务中某个问题. 服 ...
- EF CodeFirst Dome学习
创建ConsoleDome控制台应用程序 从NuGet包管理器安装EntityFramework 创建DbContextDome类并继承DbContext public class DbContext ...
- pandas-12 数学计算操作df.sum()、df.min()、df.max()、df.decribe()
pandas-12 数学计算操作df.sum().df.min().df.max().df.decribe() 常用的数学计算无非就是加减,最大值最小值,方差等等,pandas已经内置了很多方法来解决 ...
- 一个工作13年的SAP开发人员的回忆:电子科技大学2000级新生入学指南
让我们跟着Jerry的文章,一起回到本世纪初那个单纯美好的年代. 2000年9月,Jerry告别了自己的高中时代,进入到自己心目中的电子游戏大学,开始了四年的本科生活.每个新生,都拿到了这样一本薄薄的 ...
- 我的oracle 健康检查报告
最近一直想用sql来生成oracle的健康检查报告,这样看起来一目了然,经过网上搜资料加自己整理终于算是成型了,部分结果如下图所示, 具体参考附件,恳请广大网友看看是否还有需要添加的地方. DB_he ...
- 大数据之kafka-05.讲聊聊Kafka的版本号
今天聊聊kafka版本号的问题,这个问题实在是太重要了,我觉得甚至是日后能否用好kafka的关键.上一节我们介绍了kafka的几种发行版,其实不论是哪种kafka,本质上都内嵌了最核心的Apache ...