react native Expo完全基于ScrollView实现的下拉刷新和上拉触底加载
我直接封装成了一个组件
props参数为
static propTypes = {
style:PropTypes.object, // 样式
refreshing:PropTypes.bool.isRequired,//是否开始下拉刷新动画
refreshBegin: PropTypes.func,// 开始下拉刷新回调
scrollEnd: PropTypes.func,// 触底回调
};
使用示例
import React from 'react';
import {
View,
} from 'react-native';
import styles from './style';
import HomeSwiper from "../../../components/HomeSwiper";
import HomeVideo from "../../../components/HomeVideo";
import ScrollViewPull from "../../../components/ScrollViewPull";
import {connect} from 'react-redux'; // 引入connect函数
import {
getHomeSwiper,
getHomeVideoCard,
handleRefreshStatus
} from '../../../actions/homeRecommendAction'; class HomeRecommend extends React.Component {
constructor(props) {
super(props);
this.state = {
refreshing: false,
};
} componentDidMount(){
const {homeVideoCard} = this.props;
this.props.getVideoCard(undefined,1,homeVideoCard);
this.props.getSwiper();
} // 触底
handleScrollEnd = () => {
const {currentPage,lastPage,homeVideoCard} = this.props;
if(currentPage === lastPage){
this.props.getVideoCard(undefined,currentPage+1,homeVideoCard);
}
}; // 开始下拉刷新
handleRefreshBegin = () => {
const {homeVideoCard} = this.props;
this.props.getChangeRefresh(true);
this.props.getSwiper();
this.props.getVideoCard(undefined,1,homeVideoCard);
}; render() {
const {navigation,swiperData,leftVideoData,rightVideoData,refreshing} = this.props;
return (
<ScrollViewPull
style={styles.scroll}
refreshing={refreshing}
scrollEnd={()=>this.handleScrollEnd()} // 触底回调
refreshBegin={()=>this.handleRefreshBegin()} // 开始下拉刷新回调
>
<View style={styles.container}>
<HomeSwiper
navigation={navigation}
swiperData={swiperData}
/>
<View style={styles.border}/>
<HomeVideo
navigation={navigation}
leftVideoData={leftVideoData}
rightVideoData={rightVideoData}
/>
</View>
</ScrollViewPull>
);
}
} const mapStateToProps = (state) => ({
swiperData : state.homeRecommend.homeSwiperData,
currentPage : state.homeRecommend.currentPage,
homeVideoCard: state.homeRecommend.homeVideoCard,
leftVideoData : state.homeRecommend.leftVideoData,
rightVideoData : state.homeRecommend.rightVideoData,
refreshing: state.homeRecommend.refreshing
}); const mapDispatchToProps = (dispatch) => ({
getSwiper(){
dispatch(getHomeSwiper());
},
getChangeRefresh(refresh){
dispatch(handleRefreshStatus(refresh));
},
getVideoCard(id,page,homeVideoCard){
dispatch(getHomeVideoCard(id,page,homeVideoCard));
},
}); export default connect(mapStateToProps,mapDispatchToProps)(HomeRecommend);
组件全部代码为:
import React from 'react';
import {
ScrollView,
RefreshControl,
} from 'react-native';
import PropTypes from 'prop-types'; class ScrollViewPull extends React.Component {
static navigationOptions = {
header: null,
}; static propTypes = {
style:PropTypes.object, // 样式
refreshing:PropTypes.bool.isRequired,//是否开始下拉刷新动画
refreshBegin: PropTypes.func,// 开始下拉刷新回调
scrollEnd: PropTypes.func,// 触底回调
}; constructor(props) {
super(props);
this.initState();
} initState=()=>{ }; onRefresh = () => {
this.props.refreshBegin();
}; // 监听上拉触底
_contentViewScroll = (e: Object) => {
let offsetY = e.nativeEvent.contentOffset.y; //滑动距离
let contentSizeHeight = e.nativeEvent.contentSize.height; //scrollView contentSize高度
let oriageScrollHeight = e.nativeEvent.layoutMeasurement.height; //scrollView高度
if (offsetY + oriageScrollHeight >= contentSizeHeight){
this.props.scrollEnd();
}
}; render() {
const {children,refreshing,style} = this.props;
return (
<ScrollView
style={[{flex:1},style]}
showsVerticalScrollIndicator={false}
scrollToIndex
refreshControl={
<RefreshControl
refreshing={refreshing}
onRefresh={this.onRefresh}
/>
}
onMomentumScrollEnd = {this._contentViewScroll}
>
{children}
</ScrollView>
);
}
} export default ScrollViewPull;
react native Expo完全基于ScrollView实现的下拉刷新和上拉触底加载的更多相关文章
- Android 实现下拉刷新和上拉加载更多的RECYCLERVIEW和SCROLLVIEW
PullRefreshRecyclerView.java /** * 类说明:下拉刷新上拉加载更多的RecyclerView * Author: gaobaiq * Date: 2016/5/9 18 ...
- React Native 之FlatList 下拉刷新和上拉加载更多
接上一篇代码: 只修改了FlatListDemo.js里面的代码 import React, {Fragment,Component} from 'react'; import { SafeAreaV ...
- 基于iscroll.js实现下拉刷新和上拉加载特效
现在已经不是纯Android独霸天下的时代了,H5嵌入Android的Hybrid混合开发是大势所趋.今天给大家带来的就是移动端中常见的"上拉刷新,下拉加载"特效,这个特效将会基于 ...
- Android ScrollView和ListView联用,且ListView可以下拉刷新和上拉加载
ScrollView嵌套listView且ListView可以实现上拉加载. 由于代码太长,在此只提供实现思路: 先不说上拉加载的事,咱们先回想一下,ScrollView和LsitView联用,时的解 ...
- 在react中引入下拉刷新和上拉加载
1. 首先引入插件 import ReactPullLoad, {STATS} from 'react-pullload' 2. 初始化: constructor(props) { super(pro ...
- Android实战简易教程-第二十五枪(基于Baas的数据表查询下拉刷新和上拉载入实现!)
上一节我们实现了数据表的载入,可是,当数据表数据非常多时.我们就要考虑数据的分页.这里我们选用了PullToRefreshListView控件,先看一下该控件的说明: 效果图: ...
- js的下拉刷新和上拉加载,基于iScroll v4.2.5
html部分 <div id="wrapper" style="height: 100%"> <div id="scroller&q ...
- 基于SwiperJs的H5/移动端下拉刷新上拉加载更多的效果
最早时,公司的H5项目中曾用过点击一个"加载更多"的DOM元素来实现分页的功能,后来又用过网上有人写的一个上拉加载更多的插件,那个插件是页面将要滚动到底部时就自动请求数据并插入到页 ...
- 基于SwiperJs的H5/移动端下拉刷新上拉加载更多
最早时,公司的H5项目中曾用过点击一个"加载更多"的DOM元素来实现分页的功能,后来又用过网上有人写的一个上拉加载更多的插件,那个插件是页面将要滚动到底部时就自动请求数据并插入到页 ...
随机推荐
- SmartBinding工作原理分析
关于kbmMW SmartBinding,我翻译了作者写的几篇文章,其强大的绑定机制,将可视控制与各种数据源灵活绑定在一起,实现了类似DBEdit数据敏感控件的效果,可以及大的减少我们的代码,实现界面 ...
- vscode-golang跳转定义无效问题
问题发现: 本来可以进行"ctrl"+点击或者F12,进行跳转定义的方式,突然失效了 问题分析: 1.辅助工具是否关闭 解决方案,进入首选项,设置,查找go.docsTool,选项 ...
- 第一章 Django之学习Django所需知识(3)
所需编程知识 本书读者需要理解基本的面向过程和面向对象编程:流程控制(if, while 和 for),数据结构(列表,哈希表/字典),变量,类和对象. Web 开发经验,正如你所想的,也是非常有帮助 ...
- nc 命令
目录 nc 命令 一.简介 二.案例 1.端口扫描 2.聊天 3.文件传输 4.目录传输 5.加密网络发送的数据 6.流视频 7.克隆一个设备 8.打开一个shell 9.反向shell 10.指定端 ...
- 【问题】Debian安装、配置sources.list、安装VMware Tools
Debian安装: 我采用的是纯命令行安装方式.具体安装过程网上一大堆,不介绍了.需要强调一点,那个SSH Server必须选,否则像XShell这样的客户端不能访问Debian. 配置sources ...
- 排序方法——python
1.冒泡排序法(Bubble Sort) 比较相邻的元素.如果第一个比第二个大,就交换它们两个: 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对,这样在最后的元素应该会是最大的数: 针对所 ...
- 查看postgresql的日志show queries log in PostgreSQL?
原文:https://tableplus.io/blog/2018/10/how-to-show-queries-log-in-postgresql.html -------------------- ...
- [ 转载 ] Java基础
1.String a = “123”; String b = “123”; a==b的结果是什么? 这包含了内存,String存储方式等诸多知识点.ans:同样序列的字符串直接量为一个实例,所以其实引 ...
- Selenium(四)使用xpath定位元素
1.什么是xpath: 2.xpath的节点类型 3.xpath的表达式 4.开始定位 浏览器打开本地文件: (python3.7的打开语法) 查找根节点: (绝对路径)查找子节点: 查找type ...
- 如何实现UI自动化?DevExpress Winforms帮你忙
DevExpress Winforms Controls 内置140多个UI控件和库,完美构建流畅.美观且易于使用的应用程序.无论是Office风格的界面,还是分析处理大批量的业务数据,DevExpr ...