本文介紹了react-native ListView下拉刷新上拉加載實(shí)現(xiàn)。分享給大家,具體如下:
先看效果圖
下拉刷新
React Native提供了一個(gè)組件可以實(shí)現(xiàn)下拉刷新方法RefreshControl
使用方法
<ListView refreshControl={ <RefreshControl refreshing={this.state.refreshing} onRefresh={this._onRefresh.bind(this)} /> } //... </ListView>
在視圖加載的時(shí)候的時(shí)候,將refreshing設(shè)置為true,數(shù)據(jù)加載完成設(shè)置為false即可
上拉加載
利用ListView里的onEndReached方法實(shí)現(xiàn),ListView在滾動(dòng)到最后一個(gè)Cell的時(shí)候,會(huì)觸發(fā)onEndReached方法
先在ListView里添加一個(gè)Footer
render() { const FooterView = this.state.loadMore ? <View style={styles.footer}> <Text style=>加載更多...</Text> </View> : null; return <ListView refreshControl={ <RefreshControl refreshing={this.state.refreshing} onRefresh={this._onRefresh.bind(this)} /> } style={[styles.listView]} dataSource={ds.cloneWithRows(this.state.dataSource)} enableEmptySections={true} renderRow={this._renderRow.bind(this)} onEndReachedThreshold={5} onEndReached={this._onEndReached.bind(this)} renderFooter={() => FooterView} /> }
在方法_onEndReached里將Footer顯示出來(lái),在數(shù)據(jù)加載完成之后,再隱藏掉Footer
_onEndReached() { this.setState({ loadMore: true, pageNo: this.state.pageNo + 1 }); this._fetchData(); }
說(shuō)明
ListView里還設(shè)置了一個(gè)參數(shù)onEndReachedThreshold這個(gè)參數(shù)與onEndReached配合使用,它的意思是:像素的臨界值,該屬性和onEndReached配合使用,因?yàn)閛nEndReached滑動(dòng)結(jié)束的標(biāo)志是以該值作為判斷條件的
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com