import React, {Component} from 'react';
import {ActivityIndicator, FlatList, RefreshControl, StyleSheet, Text, View} from 'react-native';
import PropTypes from 'prop-types'
// RefreshList.propTypes = {
// onFetch: PropTypes.func,
// initialData:PropTypes.array,
// onChangeData:PropTypes.func,
// renderItem:PropTypes.func,
// firstShowSpinner:PropTypes.bool,
// allowInitLoad:PropTypes.bool
// }
let totalPage=0;//總的頁(yè)數(shù)
export default class RefreshList extends Component {
constructor(props){
super(props);
this.state = {
dataArray:props.initialData ?? [],
showFoot:0, // 控制foot, 0:隱藏footer 1:已加載完成,沒(méi)有更多數(shù)據(jù) 2 :顯示加載中
isRefreshing:false,//下拉控制
};
this.page = 1;
}
//組件掛載完成后執(zhí)行的方法
componentDidMount(){
let { firstShowSpinner, allowInitLoad, onFetch } = this.props
if (allowInitLoad) {
if (firstShowSpinner) {
this.onRefresh();
} else {
onFetch(1, this.refreshSuccess, this.refreshFail)
}
}
}
shouldComponentUpdate() {
return true
}
/**
* 下拉刷新 開(kāi)始
*/
onRefresh = async () => {
let { onFetch, allowRefresh } = this.props;
// this.refs && this.refs.scrollToOffset({animated:true, offset: 0 })
if (allowRefresh) {
this.setState({ isRefreshing: true })
}
onFetch(this.page, this.refreshSuccess, this.refreshFail)
};
/**
* 刷新沒(méi)有動(dòng)畫(huà)
*/
refreshWithOutAnimate = () => {
this.props.onFetch(1, this.refreshSuccess, this.refreshFail)
}
/**
* 下拉刷新成功處理
*/
refreshSuccess = (data,pageSize,pages) => {
let foot = 0;
totalPage = pages
if(this.page>=totalPage){
foot = 1;//listView底部顯示沒(méi)有更多數(shù)據(jù)了
}
this.setState({
//復(fù)制數(shù)據(jù)源
dataArray:data,
showFoot:foot,
isRefreshing:false,
});
}
/**
* 下拉刷新失敗處理
*/
refreshFail = (errorCode) => {
this.setState({refreshing: false, loadError: true, errorCode })
}
/**
* 列表數(shù)據(jù)發(fā)生變化后
*/
onChangeData = () => {
let {dataArray} = this.state;
if (this.props.onChangeData) {
this.props.onChangeData(dataArray )
}
};
/**
* 更新data數(shù)據(jù)
*/
updateData = (dataRes) => {
// console.log(dataRes)
this.setState({ data: dataRes })
};
_keyExtractor = (item, index) => index;
render() {
return (
<FlatList
data={this.state.dataArray}
renderItem={this.props.renderItem}
ListFooterComponent={this._renderFooter.bind(this)}
onEndReached={this._onEndReached.bind(this)}
onEndReachedThreshold={0.2}
ItemSeparatorComponent={this.props.showSeparator && this._separator}
keyExtractor={this._keyExtractor}
onScrollBeginDrag={() => {
// console.log('onScrollBeginDrag');
this.canAction = true;
}}
onScrollEndDrag={() => {
// console.log('onScrollEndDrag');
this.canAction = false;
}}
onMomentumScrollBegin={() => {
// console.log('onMomentumScrollBegin');
this.canAction = true;
}}
onMomentumScrollEnd={() => {
// console.log('onMomentumScrollEnd');
this.canAction = false;
}}
windowSize={300}
//為刷新設(shè)置顏色
refreshControl={
<RefreshControl
refreshing={this.state.isRefreshing}
onRefresh={this.onRefresh.bind(this)}//因?yàn)樯婕暗絫his.state
colors={['#ff0000', '#00ff00','#0000ff','#3ad564']}
progressBackgroundColor="#ffffff"
/>
}
{...this.props}
/>
);
}
_separator(){
return <View style={{height:1,backgroundColor:'#999999'}}/>;
}
_renderFooter(){
if (this.state.showFoot === 1) {
return (
<View style={{height:30,alignItems:'center',justifyContent:'flex-start',}}>
<Text style={{color:'#999999',fontSize:14,marginTop:5,marginBottom:5,}}>
沒(méi)有更多數(shù)據(jù)了
</Text>
</View>
);
} else if(this.state.showFoot === 2) {
return (
<View style={styles.footer}>
<ActivityIndicator />
<Text>正在加載更多數(shù)據(jù)...</Text>
</View>
);
} else if(this.state.showFoot === 0){
return (
<View style={styles.footer}>
<Text></Text>
</View>
);
}
}
_onEndReached(){
if(!this.canAction) return
console.log('end reach');
//如果是正在加載中或沒(méi)有更多數(shù)據(jù)了,則返回
if(this.state.showFoot != 0 || this.state.isRefreshing ){
return ;
}
//如果當(dāng)前頁(yè)大于或等于總頁(yè)數(shù),那就是到最后一頁(yè)了,返回
if((this.page!=1) && (this.page>=totalPage)){
return;
} else {
this.page++;
}
//底部顯示正在加載更多數(shù)據(jù)
this.setState({showFoot:2});
//獲取數(shù)據(jù),在componentDidMount()已經(jīng)請(qǐng)求過(guò)數(shù)據(jù)了
if (this.page>1){
this.props.onFetch(this.page, this.loadMoreSuccess, this.loadMoreFail)
}
}
/**
* 加載更多成功處理
*/
loadMoreSuccess = (resData, pageSize = 10) => {
let { dataArray } = this.state;
let foot = 0
if (resData.length < pageSize || resData.length == 0) {
foot = 1
}
let merData = [...dataArray, ...resData]
this.setState({ dataArray: merData, showFoot: foot }, () => {
this.onChangeData();
});
}
/**
* 加載更多失敗處理
*/
loadMoreFail = (errorCode) => {
console.log(errorCode);
this.setState({ showFoot: 0 })
}
}
const styles = StyleSheet.create({
container: {
flex: 1
},
center:{
justifyContent:'center',alignItems: 'center'
},
oneBtn: {
padding: 5,
borderRadius: 5,
},
oneBox: {
flexDirection: 'row',
alignItems: 'center',
},
footer:{
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
paddingVertical:10
}
});
Refreshlist下拉刷新組件封裝
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 上一篇,我們實(shí)現(xiàn)了h5 實(shí)現(xiàn)向左平滑,出現(xiàn)按鈕操作,封裝組件,模擬購(gòu)物車(chē)左滑刪除,現(xiàn)在我們將要實(shí)現(xiàn)一個(gè)下拉刷新的組...
- betterScroll.js 原理 betterScroll.js 滾動(dòng)原理。 -- betterScroll...
- 本人正在基于 vue2.0 + webpack + es6 搭建前端架構(gòu),整理了部分插件,下面這個(gè)是下拉更新 上拉...
- Github地址:vpull-refresh 如何使用 將項(xiàng)目中的src/components/pullRefre...