react-native 自定义 下拉刷新 / 上拉加载更多 组件
1.封装 Scroller 组件
/*** 下拉刷新/上拉加载更多 组件(Scroller)*/ import React, {Component} from 'react'; import {StyleSheet,Text,View,ListView,ActivityIndicator,RefreshControl, } from 'react-native';export default class Scroller extends Component {// 构造函数 constructor(props) {super(props);this.state = {// }}render() {const { dataSource, renderRow, isRefreshing } = this.props;// console.log(this.props);return (<View style={styles.container}>{/*列表数据*/}<ListView// 数据源dataSource={dataSource}// 从数据源(dataSource)中接受一条数据,以及它和它所在section的IDrenderRow={renderRow}// 页头与页脚会在每次渲染过程中都重新渲染(允许在ListView底部增加一栏,便于显示加载动画)renderFooter={this._renderFooter.bind(this)}// 当所有的数据都已经渲染过,并且列表被滚动到距离最底部不足onEndReachedThreshold个像素的距离时调用onEndReached={this._fetchMoreData.bind(this)}// 调用onEndReached之前的临界值,单位是像素。(预加载)onEndReachedThreshold={20}// 隐藏右侧滚动条showsVerticalScrollIndicator={false}// finished warning : in next release ...enableEmptySections={true}// 自动调整迁移内容// 导航栏或标签栏或工具栏不覆盖 Scrollview 内容// 去除默认定位间距automaticallyAdjustContentInsets={false}// 下拉刷新refreshControl={<RefreshControl// 是否刷新refreshing={isRefreshing}onRefresh={this._onRefresh.bind(this)}tintColor={"#ff6600"}title={"拼命加载中..."}/>}/></View>)}/*** 下拉刷新*/_onRefresh() {// console.log('下拉刷新');if (this.props.isRefreshing || !this._hasMore()) {return}// 向后台发送 '0',告知刷新操作this.props.fetchData(0);}/*** 加 _ 代表私有方法* 上拉加载更多*/_fetchMoreData() {// console.log('上拉加载更多');/*** this._hasMore() 验证还有更多数据* isLoadingTail true/false 加载动画(菊花图)*/if (!this._hasMore() || this.props.isLoadingTail) {return}let page = this.props.cachedResults.nextPage;this.props.fetchData(page);}/*** 验证还有更多数据*/_hasMore() {return this.props.cachedResults.items.length !== this.props.cachedResults.items.total;}/*** 底部加载动画 及 没有更多数据文本(ListView底部增加一栏,便于显示加载动画)*/_renderFooter() {if (!this._hasMore() && this.props.cachedResults.total !== 0) {return (<View style={styles.loadingMore}><Text style={styles.loadingText}>没有更多了</Text></View>)}if (!this.props.isLoadingTail) {return (<View style={styles.loadingMore}></View>)}// 菊花图return (<ActivityIndicator style={styles.loadingMore}/>)}}// 样式 const styles = StyleSheet.create({container: {flex: 1,backgroundColor: '#F5FCFF',},// 菊花图 loadingMore: {marginVertical: 20},// 文案样式 loadingText: {color: '#777',textAlign: 'center'} });
2.页面调用
/*** 视频列表页*/ import React, {Component} from 'react'; import {StyleSheet,Text,View,ImageBackground,ListView,TouchableHighlight,Alert,Dimensions,ActivityIndicator,RefreshControl, } from 'react-native'; // 下拉刷新/上拉加载更多组件 import Scroller from '../../components/Scroller'; // 图标 import Icon from 'react-native-vector-icons/Ionicons'; // item 组件 import CreationItem from '../../components/CreationItem'; import config from '../../common/config'; import request from '../../common/request';let {width} = Dimensions.get("window");// 缓存列表中所有数据 let cachedResults = {nextPage: 1, // 下一页items: [], // listview 数据(视频列表)total: 0 // 总数 };export default class List extends Component {// 构造函数 constructor() {super();let ds = new ListView.DataSource({// 比较两条数据是否是一样的,来判断数据是否发生改变rowHasChanged: (r1, r2) => r1 !== r2});this.state = {dataSource: ds.cloneWithRows([]),isLoadingTail: false, // loading?isRefreshing: false // refresh? }}render() {return (<View style={styles.container}>{/*顶部标题栏*/}<View style={styles.header}><Text style={styles.headerTitle}>列表页面</Text></View>{/*列表数据*/}<Scroller// 数据源dataSource={this.state.dataSource}// 渲染item(子组件)renderRow={this._renderRow.bind(this)}// 是否可以刷新isRefreshing={this.state.isRefreshing}// 是否可以加载更多isLoadingTail={this.state.isLoadingTail}// 请求数据fetchData={this._fetchData.bind(this)}// 缓存列表数据cachedResults={cachedResults}/></View>)}// 生命周期-组件挂载完毕 请求数据 componentDidMount() {this._fetchData(1);}// 请求数据 _fetchData(page) {let that = this;if (page !== 0) { // 加载更多操作this.setState({isLoadingTail: true});} else { // 刷新操作this.setState({isRefreshing: true});// 初始哈 nextPagecachedResults.nextPage = 1;}request.get(config.api.base + config.api.creations, {accessToken: 'abc'})// data 变化的新数据.then((data) => {if (data.success) {// 保存原数据let items = cachedResults.items.slice();if (page !== 0) { // 加载更多操作// 数组拼接items = items.concat(data.data);cachedResults.nextPage += 1;} else { // 刷新操作// 数据不变items = data.data;}cachedResults.items = items; // 视频列表数据cachedResults.total = data.total; // 总数 setTimeout(function () {if (page !== 0) { // 加载更多操作 that.setState({isLoadingTail: false,dataSource: that.state.dataSource.cloneWithRows(cachedResults.items)});} else { // 刷次操作 that.setState({isRefreshing: false,dataSource: that.state.dataSource.cloneWithRows(cachedResults.items)});}}, 1000);}}).catch((error) => {if (page !== 0) { // 上拉加载更多操作this.setState({isLoadingTail: false});} else {this.setState({ // 刷新操作isRefreshing: false});}console.error(error);});}// 列表 Item _renderRow(row) {const { navigation } = this.props;return (<CreationItemnavigation={navigation}key={row.id} // 子组件唯一性row={row}/>)} }// 样式 const styles = StyleSheet.create({container: {flex: 1,backgroundColor: '#F5FCFF',},// 头部样式 header: {paddingTop: 25,paddingBottom: 12,backgroundColor: '#ee735c',},// 头部title样式 headerTitle: {color: '#fff',fontSize: 16,textAlign: 'center',fontWeight: '600'},// 菊花图 loadingMore: {marginVertical: 20},// 文案样式 loadingText: {color: '#777',textAlign: 'center'} });
转载于:https://www.cnblogs.com/qiyecao/p/9638189.html
react-native 自定义 下拉刷新 / 上拉加载更多 组件相关推荐
- uni-app下拉刷新触底加载更多
首先在pages.json 配置文件中配置 "enablePullDownRefresh": true 需要在哪用加载就配置在路由的style里 两个事件 //下拉刷新 o ...
- recyclerview的数据刷新(下拉刷新和自动加载更多)以及添加提示语(例如:“数据已加载完毕”)
下拉加载更多的核心是SwipeRefreshLayout搭配Recyclerview进行使用.布局为 <android.support.v4.widget.SwipeRefreshLayout ...
- Android 自定义 ListView 上下拉动“刷新最新”和“加载更多”歌曲列表
本文内容 环境 测试数据 项目结构 演示 参考资料 本文演示,上拉刷新最新的歌曲列表,和下拉加载更多的歌曲列表.所谓"刷新最新"和"加载更多"是指日期.演示代码 ...
- 使用MJRefresh自定义下拉刷新,上拉加载动画
有时候我们需要自己设置下拉刷新,上拉加载动画的实现,这里主要是记录下使用MJRefresh自定义下拉刷新,上拉加载动画..... 下拉刷新我们只需要继承MJRefreshGifHeader即可: 实现 ...
- react-native 自定义 下拉刷新 / 上拉加载更多 组件
1.封装 Scroller 组件 src/components/Scroller/index.js /*** 下拉刷新/上拉加载更多 组件(Scroller)*/ import React, {Com ...
- 分享轮子-flutter下拉刷新上拉加载
flutter下拉上拉组件轮子 什么是flutter? 首先说下flutter,估计这个应该挺多人没听过flutter这个框架,它是一个google推出的跨平台的移动应用UI框架,和React Nat ...
- react-native ListView 封装 实现 下拉刷新/上拉加载更多
1.PageListView 组件封装 src/components/PageListView/index.js /*** 上拉刷新/下拉加载更多 组件*/ import React, { Compo ...
- Android ListView 实现下拉刷新上拉加载
转载请注明出处:http://blog.csdn.net/allen315410/article/details/39965327 1.简介 无疑,在Android开发中,ListView是使用非常频 ...
- 微信小程序下拉刷新/上拉加载组件
简介 一款基于官方组件scroll-view进行封装的支持上拉加载下拉刷新的微信小程序组件,需要基础库2.10.1及以上, 项目地址:github 预览 功能 下拉刷新 上拉加载 支持自定义下拉样式 ...
- Android自定义控件实战——实现仿IOS下拉刷新上拉加载 PullToRefreshLayout
下拉刷新控件,网上有很多版本,有自定义Layout布局的,也有封装控件的,各种实现方式的都有.但是很少有人告诉你具体如何实现的,今天我们就来一步步实现自己封装的 PullToRefreshLayout ...
最新文章
- JAVA 算法练习(二)
- Opencv腐蚀操作去除激光反光光斑
- mysql create at_create_at update_at
- Ubuntu16.04下arm-linux-gcc交叉编译环境搭建
- sublime配置随笔提示
- python面试经典题_16道Python经典面试题及答案
- 在.net2.0中实现Action和Func方法
- 此地址使用了一个通常用于网络浏览以外的端口。出于安全原因,Firefox 取消了该请求。...
- linux 下按内容查找文件
- Bootstrap进度条的颜色
- 自定义广播增加权限控制
- 2017百度之星资格赛:1005. 寻找母串(卡特兰数+分块打表)
- python基于scrapy框架爬取当当图书信息
- win10輸入法去掉语言栏?win10輸入法切换简体繁体?
- Nagios汉化页面
- 小篮子玩意儿、你苏爷就是扣字神话不服气么。
- 二分图的最大匹配-解决匹配问题
- labview调用python 开发视觉_龙哥教你学视觉—tensorflow目标检测LabVIEW深度学习教程...
- 教你如何下载在线视频
- Desire(G7) 联通3g上网与彩信的设置方法
热门文章
- 探索webpack热更新对代码打包结果的影响(二)
- 忽略“Signal: SIGSEGV (Segmentation fault)”
- www计算机会议影响力
- 【PHP代码审计】 那些年我们一起挖掘SQL注入 - 4.全局防护Bypass之二次注入
- 解决UnicodeEncodeError: 'ascii' codec can't encode characters in position 问题(转)
- UEdit初始化加载内容偶尔失败,解决
- Yii2修改默认布局
- Some Important Data Structures
- JQuery操作SharePoint Web Services之添加列表数据
- JVM到底怎么进行类加载器的呢?