React Ant Design Mobile ListView 上拉刷新,下拉加載


import React from 'react'
import './earnings.scss'
import { ListView, PullToRefresh } from 'antd-mobile';
export default class Earnings extends React.Component {
    constructor(props) {
        super(props)
        const dataSource = new ListView.DataSource({
            rowHasChanged: (r1, r2) => r1 !== r2
        });
        this.state = {
            dataSource,
            firstArr:['蘋果','栗子','橘子','桃子','葡萄','蘋果1','栗子2','橘子3','桃子4','葡萄5','蘋果6','栗子7','橘子8','桃子9','葡萄0'],
            firstArr1:['蘋果s','栗子ss','橘子ssss','桃子sss','葡萄sss','蘋果1sss','栗子2sss','橘子3ss','桃子4ss','葡萄ss5'],
            refreshing: false, //上拉刷新
            isLoading: true, //數據是否加載完畢
            num:0
        }
    }
    componentDidMount(){
        this.setState({
            dataSource: this.state.dataSource.cloneWithRows({...this.state.firstArr})
        })
    }
    //下拉刷新
    onRefresh = () => {
        this.setState({ refreshing: true })
        //接口請求第一頁數據,完成后將refreshing設為false
        setTimeout(()=>{
            this.setState({
                refreshing: false
            })
        },2000)
    }
    // 滑動到底部時加載更多
    onEndReached = (event) => {
        // 顯示加載loading....
        this.setState({
            isLoading: false 
        })
        
        // 當this.state.num 》 1時,規定為數據加載完畢
        if(this.state.num > 1){
            this.setState({
                isLoading : true
            })
            return false
        }

        // 等待2s后再原始數據上追加數據
        setTimeout(() => {
            this.setState({
                firstArr: [...this.state.firstArr,...this.state.firstArr1]
            },()=>{
                // 數據發生改變是要使用cloneWithRows通知
                this.setState({
                    num: this.state.num += 1,
                    dataSource: this.state.dataSource.cloneWithRows({...this.state.firstArr}),
                })
                console.log(this.state)
            })
        },2000)
    }
    render() {
        const row = (rowData, sectionID, rowID) => {
            // 這里rowData,就是上面方法cloneWithRows的數組遍歷的單條數據了,直接用就行
            // console.log('rowData', rowData);
            // console.log('sectionID', sectionID);
            // console.log('rowID', rowID);
            return (
                <div className="earnings">
                    <div className="content">
                        <span className="date box">{rowData}</span>
                        <span className="money box">0.30元</span>
                    </div>
                </div>
            )
        }
        return (
            <div className="earnings-list">
                <ListView
                    dataSource={this.state.dataSource}
                    renderRow={row}
                    initialListSize={15}
                    pageSize={10}
                    onEndReached={this.onEndReached}
                    onEndReachedThreshold={10}
                    useBodyScroll={false}
                    renderHeader={() => 
                        (
                            <div className="earnings-title">
                                <span>日期</span>
                                <span>好友貢獻獎勵</span>
                            </div>
                        )
                    }
                    renderFooter={() => (<div style={{ padding: 30, textAlign: 'center' }}>
                        { !this.state.isLoading ? '加載中....' : '我是有底線的'}
                    </div>)}
                    style={{ width: '100vw', height: '100vh' }}
                    pullToRefresh={<PullToRefresh // import { PullToRefresh } from 'antd-mobile'
                        refreshing={this.state.refreshing}
                        onRefresh={this.onRefresh}
                    />}
                />
            </div>
        )
    }
}

 

.earnings-list{
    background: #ffffff;
    .earnings{
        h1{
            margin: 0;
        }
        padding: .2rem;
        .content{
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #EEEEEE;
            line-height: .8rem;
            padding: 0 .4rem;
            .box{
                font-size: .28rem;
            }
            .date{
                color: #333333;
            }
            .money{
                color: #FF7841;
            }
        }
    }
    .am-list-header{
        background: #ffffff;
    }
    .am-list-body::after{
        background-color: #ffffff !important;
    }
    .am-list-body::before{
        background-color: #ffffff !important;
    }
    .earnings-title{
        display: flex;
        align-items: center;
        justify-content: space-between;
        border-bottom: 1px solid #EEEEEE;
        line-height: .8rem;
        padding: 0 .2rem;
        span{
            color: #999999;
            font-size: .24rem;
        }
    }
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM