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元素来实现分页的功能,后来又用过网上有人写的一个上拉加载更多的插件,那个插件是页面将要滚动到底部时就自动请求数据并插入到页 ...
随机推荐
- 最最常用的RAID
若转载请于明显处标明出处:http://www.cnblogs.com/kelamoyujuzhen/p/8980696.html RAID stands for Redundant Array of ...
- python之分支和循环
Day 1-night 三元操作符 语法:a=x if 条件 else y 即:当条件为True时,a的值赋值为x,否则赋值为y eg:small=x if x<y else y <=& ...
- python-----多线程笔记
多进程笔记: 多线程介绍: 多线程是为了同步完成多项任务,通过提高资源使用效率来提高系统的效率.线程是在同一时间需要完成多项任务的时候实现的. 最简单的比喻多线程就像火车的每一节车厢,而进程则是火车. ...
- JSONPlaceholder - 免费的在线REST服务(提供测试用的HTTP请求假数据)
http://www.hangge.com/blog/cache/detail_2020.html 一.GET 方式请求数据1,获取文章(贴子)数据 二.POST 方式请求数据1,功能说明我们可以将数 ...
- java实现网络请求超时自动熔断
原文:https://www.jianshu.com/p/326465500d1c?utm_campaign 使用场景 之前在实现熔断降级组件时,需要实现一个接口的超时中断,意思是,业务在使用熔断降级 ...
- No result defined for action com.java.test.Action.HelloAction and result index
Struts中配置action访问出错: Struts Problem Report Struts has detected an unhandled exception: Messages: No ...
- less-important
!important关键字:会为所有混合带来的样式,添加!important 在css里面加上!important,是所有样式优先级最高的 在less里面什么场景会用important,在调试的时候 ...
- 定义一个类:实现功能可以返回随机的10个数字,随机的10个字母, 随机的10个字母和数字的组合;字母和数字的范围可以指定,类似(1~100)(A~z)
#习题2:定义一个类:实现功能可以返回随机的10个数字,随机的10个字母, #随机的10个字母和数字的组合:字母和数字的范围可以指定 class RandomString(): #随机数选择的范围作为 ...
- [转]python3 跨目录模块调用,你真的懂了吗?
小伙伴们,你们有遇到过调用自己写的模块(跨目录模块调用),提示你ImportError:No module named ...的情况,如果有,而且到现在还没有搞明白的,我想说,你今天看对文章了. 这篇 ...
- pyside pyqt QPushbuttion 无边框 stylesheet border:none
pyside pyqt QPushbuttion 无边框 stylesheet border:none 在 stylesheet 中添加 border:none 即可 效果是字体到边缘之间的间隙为0, ...