Tools
首页
画图
音乐
采集
记事
博客
实验室
登录
lypeng
146
文章
11
分类
46
记事
分类
生活-[23]
Linux-[24]
前端-[9]
数据库-[16]
PHP-[31]
git-[7]
其他-[6]
python-[20]
算法-[4]
React-Native-[4]
中草药-[2]
广告位1
广告位2
首页
/ React-Native
返回列表
react-native实现下拉刷新与上拉加载
阅读:1639
发布:2019-04-29
作者:lypeng
效果图:
扫二维码下载apk手机预览 (点击栏目页任何一个分类进去预览真实效果)  以上apk代码地址:https://github.com/lypeng29/RN_news?_blank 上代码: ``` import React from 'react' import { Dimensions, FlatList, View, Text, StyleSheet, ActivityIndicator } from 'react-native' // get请求 var util = { getRequest: function (url, successcallback, failcallback) { fetch(url) .then((response) => { if (response.ok) { return response.json() } else { alert('服务器繁忙,请稍后再试;\r\nCode:' + response.status) } }) .then((responseData) => successcallback(responseData)) .catch((error) => failcallback(error)) } } // 屏幕尺寸 var screenInfo = { width: Dimensions.get("window").width, height: Dimensions.get("window").height }; export default class FlatListPage extends React.Component { constructor(props) { super(props) this.state = { refresh: false, listData: [], animating: true, nomore: 1, page:1 } } getNewData() { this.setState({ refresh: true }) let newData = [] for (let i = 0; i < 2; i++) { newData.push({'title':'new data'}) } setTimeout(() => { this.setState({ refresh: false, listData: [...newData, ...this.state.listData] }) }, 1500) } componentDidMount() { // this.onEndReachedCalled = false; this.getData() } getData() { let newData = []; if(this.state.nomore == 1){ // 发起请求 var that = this; util.getRequest('https://www.dpshop.net/article/index?cid=12&page=' + this.state.page,function(res){ res.data.map((item, i) => { newData.push(item); }); that.setState({ listData: [...that.state.listData, ...newData], nomore: res.pagination.more, page: that.state.page +1 }) },function(res){ alert(res); }) } } // ListEmptyComponent(){ // if(this.state.empty == false){ // return ( //
内容加载中...
// ) // }else{ // return ( //
没有数据~
// ) // } // } ListFooterComponent = () => { return (
{ this.state.listData.length != 0 ? this.state.nomore==0 ? (
- 我是有底线的 -
) : (
加载更多...
) : null }
); } _renderItem(item) { return (
{item.title}
) }; render() { return (
this._renderItem(item)} // renderItem={({ item }) =>
{item.title}
} // 下拉刷新数据 refreshing={this.state.refresh} onRefresh={() => { this.getNewData() }} // 上拉加载更多数据 onEndReachedThreshold={.2} onEndReached={() => { this.getData() }} // ListEmptyComponent={
暂无数据
} // ListEmptyComponent={this.ListEmptyComponent.bind(this)} // ListFooterComponent={
获取更多数据
} ListFooterComponent={this.ListFooterComponent.bind(this)} // ItemSeparatorComponent={ItemSeparatorComponent} // 列表key值,必须是字符串 keyExtractor={(item, index) => index.toString()} //设置下拉加载更多的指示器的位置 // progressViewOffset={50} />
) } } const styles = StyleSheet.create({ item: { backgroundColor: "#e8e8e8", marginBottom: 10, height: 150, lineHeight: 150, textAlign: "center", color:'#000', fontSize:16 }, baseLine: { width: screenInfo.width, height: 1, backgroundColor: '#eeeeee', }, noListView: { width: screenInfo.width, height: screenInfo.height - 140, justifyContent: 'center', alignItems: 'center', }, NoListText: { marginTop: 15, fontSize: 18, color: '#999999', }, bottomfoot: { flexDirection: 'row', justifyContent: 'center', alignItems: 'center', padding: 10, }, footText: { marginTop: 5, fontSize: 12, color: '#999999', }, activeLoad: { flexDirection: 'row', justifyContent: 'center', alignItems: 'center', }, ml: { marginLeft: 10, }, }) ```
------本文结束
感谢阅读------
上一篇:
解决React-native一行两个,一行三个,一行N个布局问题
下一篇:没有了