React native ListView 增加頂部下拉刷新和底下點(diǎn)擊刷新示例
1. 底部點(diǎn)擊刷新
1.1 先增加一個(gè)按鈕

render() {
if(!this.state.data){
return(
<Text>Loading... </Text>
)
}else{
return(
<ListView
refreshControl={
<RefreshControl
refreshing = {false}
onRefresh = {this.reloadWordData.bind(this)}
/>
}
dataSource={this.state.data}
renderRow={(rowData)=>this.renderRow(rowData)}
renderFooter={this.renderFooter.bind(this)}
>
</ListView>
);
}
}
renderFooter(){
return (
<View style={{marginVertical: 10, marginBottom:20}} >
<Button
onPress={this.addMoreOnFoot.bind(this)}
title="點(diǎn)擊載入更多"
/>
</View>
)
}
給ListView 增加一個(gè)renderFooter 方法來繪制底部元素。在里面顯示一個(gè)按鈕。
按鈕處理邏輯:
addMoreOnFoot(){
// alert('addMoreOnFoot')
// console.log('addMoreOnFoot')
const url = 'http://127.0.0.1/getFootContent?lastid=' + this.state.footLastId + '&count=20&isTop=0'
fetch(url)
.then((response)=>response.json())
.then((jsondata)=>{
if (jsondata.data && jsondata.data.length > 0){
const rowData = this.state.jsondata.concat(jsondata.data);
this.setState({
footLastId:jsondata.data[jsondata.data.length - 1]['id'],
jsondata:rowData,
data:new ListView.DataSource({rowHasChanged:(r1, r2) => r1 != r2}).cloneWithRows(rowData),
})
}
})
.catch((error)=>{
alert(error);
});
}
點(diǎn)擊后進(jìn)行網(wǎng)絡(luò)處理,把之前最后一條id也傳給服務(wù)器,讓服務(wù)器返回這個(gè)id后面的20條記錄。然后重新setState即可。
2. 頭部下拉刷新
ListView中增加RefeshControl
render() {
if(!this.state.data){
return(
<Text>Loading... </Text>
)
}else{
return(
<ListView
refreshControl={
<RefreshControl
refreshing = {false}
onRefresh = {this.reloadWordData.bind(this)}
/>
}
dataSource={this.state.data}
renderRow={(rowData)=>this.renderRow(rowData)}
renderFooter={this.renderFooter.bind(this)}
>
</ListView>
);
}
}
載入最新的頭部數(shù)據(jù)添加到this.State中
reloadWordData(){
// alert(this.state.topLastId)
const url = 'http://127.0.0.1/getFootContent?lastid=' + this.state.topLastId + '&count=20&isTop=1'
fetch(url)
.then((response)=>response.json())
.then((jsondata)=>{
if (jsondata.data && jsondata.data.length > 0){
const rowData = jsondata.data.concat(this.state.jsondata);
this.setState({
topLastId:jsondata.data[0]['id'],
jsondata:rowData,
data:new ListView.DataSource({rowHasChanged:(r1, r2) => r1 != r2}).cloneWithRows(rowData),
})
}
})
.catch((error)=>{
alert(error);
});
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用React實(shí)現(xiàn)一個(gè)簡單的待辦事項(xiàng)列表的示例代碼
這篇文章我們將詳細(xì)講解如何建立一個(gè)這樣簡單的列表,文章通過代碼示例介紹的非常詳細(xì),對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-08-08
react-navigation之動(dòng)態(tài)修改title的內(nèi)容
這篇文章主要介紹了react-navigation之動(dòng)態(tài)修改title的內(nèi)容,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
Hello?React的組件化方式之React入門小案例演示
這篇文章主要介紹了Hello?React的組件化方式-React入門小案例,本文通過Hello?React的案例,?來體驗(yàn)一下React開發(fā)模式,?以及jsx的語法,需要的朋友可以參考下2022-10-10
如何應(yīng)用?SOLID?原則在?React?中整理代碼之開閉原則
React?不是面向?qū)ο?,但這些原則背后的主要思想可能是有幫助的,在本文中,我將嘗試演示如何應(yīng)用這些原則來編寫更好的代碼,對(duì)React?SOLID原則開閉原則相關(guān)知識(shí)感興趣的朋友一起看看吧2022-07-07
詳解基于React.js和Node.js的SSR實(shí)現(xiàn)方案
這篇文章主要介紹了詳解基于React.js和Node.js的SSR實(shí)現(xiàn)方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03

