Home  >  Article  >  Web Front-end  >  Detailed explanation of the case of adding top pull-down refresh and bottom click refresh in React native ListView on mobile terminal

Detailed explanation of the case of adding top pull-down refresh and bottom click refresh in React native ListView on mobile terminal

php中世界最好的语言
php中世界最好的语言Original
2018-05-15 10:36:421434browse

This time I will bring you a detailed explanation of the case of adding top pull-down refresh and bottom click refresh in React native ListView on the mobile terminal. What are the precautions for adding top pull-down refresh and bottom click refresh in React native ListView on the mobile terminal? Here are Let’s take a look at practical cases.

1. Click refresh at the bottom

1.1 First add a button

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="点击载入更多"
      />
    </View>
   )
 }

Add a renderFooter method to ListView to draw the bottom element. Display a button inside.

Button processing logic:

addMoreOnFoot(){
 // alert(&#39;addMoreOnFoot&#39;)
 // console.log(&#39;addMoreOnFoot&#39;)
 const url = &#39;http://127.0.0.1/getFootContent?lastid=&#39; + this.state.footLastId + &#39;&count=20&isTop=0&#39;
 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][&#39;id&#39;],
    jsondata:rowData,
    data:new ListView.DataSource({rowHasChanged:(r1, r2) => r1 != r2}).cloneWithRows(rowData),
   })
   }
 })
 .catch((error)=>{
  alert(error);
 });
}

After clicking, network processing is performed, and the last id is also passed to the server, and the server returns the 20 records after this id. Then resetState.

2. Head pull-down refresh

Add RefeshControl in ListView

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>
 
   );
  }
 }

Load the latest header data and add it to this.State

reloadWordData(){
  // alert(this.state.topLastId)
  const url = &#39;http://127.0.0.1/getFootContent?lastid=&#39; + this.state.topLastId + &#39;&count=20&isTop=1&#39;
  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][&#39;id&#39;],
     jsondata:rowData,
     data:new ListView.DataSource({rowHasChanged:(r1, r2) => r1 != r2}).cloneWithRows(rowData),
    })
    }
  })
  .catch((error)=>{
   alert(error);
  });
 }

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the steps to implement dynamic and static paging in layui

Detailed explanation of the steps to implement angular routing highlighting

The above is the detailed content of Detailed explanation of the case of adding top pull-down refresh and bottom click refresh in React native ListView on mobile terminal. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn