最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

React-Native實(shí)現(xiàn)ListView組件之上拉刷新實(shí)例(iOS和Android通用)

 更新時(shí)間:2017年07月11日 16:55:40   作者:coder_小刀  
本篇文章主要介紹了React-Native實(shí)現(xiàn)ListView組件之上拉刷新實(shí)例(iOS和Android通用),具有一定的參考價(jià)值,有興趣的可以了解一下

在web應(yīng)用中,上拉刷新加載更多,下拉刷新列表的操作非常常見(jiàn),那么在React-Native中是如何實(shí)現(xiàn)呢,我們具體來(lái)看一下
ReactNative提供了RefreshControl下拉刷新組件,但是沒(méi)有提供上拉刷新組件,上拉刷新在App中是很常用的。

今天我們來(lái)實(shí)現(xiàn)一個(gè)iOS和Android通用的上拉刷新功能。

下面簡(jiǎn)要介紹下我實(shí)現(xiàn)的思路。

思路:

1、常量定義:

const moreText = "加載完畢"; //foot顯示的文案 
//頁(yè)碼 
var pageNum = 1; 
//每頁(yè)顯示數(shù)據(jù)的條數(shù) 
const pageSize = 10; 
//頁(yè)面總數(shù)據(jù)數(shù) 
var pageCount = 0; 
//頁(yè)面List總數(shù)據(jù) 
var totalList = new Array(); 
 
//foot: 0 隱藏 1 已加載完成 2 顯示加載中 

2、定義ListView

<ListView 
 enableEmptySections={true} 
 dataSource={this.state.dataSource} 
 renderRow={this._renderRow.bind(this)} 
 renderFooter={this._renderFooter.bind(this)} 
 onEndReached={this._endReached.bind(this)} 
 onEndReachedThreshold={0} 
/> 

3、聲明State狀態(tài)機(jī)變量

ListView.DataSource實(shí)例(列表依賴(lài)的數(shù)據(jù)源)

constructor(props) { 
 super(props); 
 this.state = { 
  dataSource: new ListView.DataSource({ 
   rowHasChanged: (r1, r2) => r1 !== r2, 
  }), 
  loaded: false,//控制Request請(qǐng)求是否加載完畢 
  foot:0,// 控制foot, 0:隱藏foot 1:已加載完成 2 :顯示加載中 
  error:false, 

這里我們主要聲明了dataSource,這個(gè)沒(méi)什么說(shuō)的

  1. loaded:用來(lái)控制整個(gè)頁(yè)面的菊花
  2. error:如果Request錯(cuò)誤,顯示一個(gè)錯(cuò)誤頁(yè)面
  3. foot: 控制Footer的view

4、渲染頁(yè)面前,加載數(shù)據(jù)

componentWillMount() { 
 this._fetchListData(); 
} 

5、Load服務(wù)端數(shù)據(jù)

_fetchListData() { 
 if(pageNum > 1){ 
  this.setState({loaded:true}); 
 } 
 fetch(requestURL, { 
  method: 'get', 
  headers: headerObj, 
 }).then(response =>{ 
  if (response.ok) { 
   return response.json(); 
  } else { 
   this.setState({error:true,loaded:true}); 
  } 
 }).then(json=>{ 
  let responseCode = json.code; 
  if (responseCode == 0) { 
   let responseData = json.data; 
 
   pageCount = responseData.count; 
   let list = responseData.data; 
 
   if (orderList == null) { 
    orderList = []; 
    currentCount = 0; 
   } else { 
    currentCount = list.length; 
   } 
   if(currentCount < pageSize){ 
    //當(dāng)當(dāng)前返回的數(shù)據(jù)小于PageSize時(shí),認(rèn)為已加載完畢 
    this.setState({ foot:1,moreText:moreText}); 
   }else{//設(shè)置foot 隱藏Footer 
    this.setState({foot:0}); 
   } 
   for (var i=0; i < list.length; i++) { 
    totalList.push( list[i] ); 
   } 
 
   this.setState({ 
    dataSource: this.state.dataSource.cloneWithRows(totalList), 
    loaded: true, 
   }); 
  }else{ 
   this.setState({error:true,loaded:true}); 
  } 
 }).catch(function (error) { 
  this.setState({error:true,loaded:true}); 
 }); 
} 

這里的細(xì)節(jié)挺多的:

1、當(dāng)pageNum > 1時(shí),就不要整個(gè)頁(yè)面的菊花,此時(shí)loaded一直為true,這個(gè)主要是為了頁(yè)面效果,要不然沒(méi)加載一頁(yè)數(shù)據(jù),這個(gè)屏幕就會(huì)閃一下。

2、比較當(dāng)前返回的list的大小,是否小于pageSize,控制Footer是否隱藏,還是顯示已加載完畢

3、聲明了一個(gè)全局的totalList對(duì)象,每次有新數(shù)據(jù)的時(shí)候,都push進(jìn)去。

如果不采用push的方式的話(huà),直接采用setState方法的話(huà),第二頁(yè)會(huì)把第一頁(yè)的數(shù)據(jù)覆蓋掉。

6、定義renderRow方法

renderRow={this._renderRow.bind(this)}   列表組件渲染函數(shù) ,此處頁(yè)面邏輯省略。

7、定義renderFooter方法

renderFooter   頁(yè)腳會(huì)在每次渲染過(guò)程中都重新渲染。

_renderFooter() { 
 if(this.state.foot === 1){//加載完畢 
  return ( 
  <View style={{height:40,alignItems:'center',justifyContent:'flex-start',}}> 
   <Text style={{color:'#999999',fontSize:12,marginTop:10}}> 
    {this.state.moreText} 
   </Text> 
  </View>); 
 }else if(this.state.foot === 2) {//加載中 
  return ( 
  <View style={{height:40,alignItems:'center',justifyContent:'center',}}> 
   <Image source={{uri:loadgif}} style={{width:20,height:20}}/> 
  </View>); 
 } 
} 

根據(jù)狀態(tài)機(jī)變量foot控制Footer的顯示

8、onEndReached 定義

onEndReachedThreshold={0}

當(dāng)所有的數(shù)據(jù)都已經(jīng)渲染過(guò),并且列表被滾動(dòng)到距離最底部不足onEndReachedThreshold個(gè)像素的距離時(shí)調(diào)用。原生的滾動(dòng)事件會(huì)被作為參數(shù)傳遞。譯注:當(dāng)?shù)谝淮武秩緯r(shí),如果數(shù)據(jù)不足一屏(比如初始值是空的),這個(gè)事件也會(huì)被觸發(fā)

_endReached(){ 
 if(this.state.foot != 0 ){ 
 return ; 
 } 
 this.setState({ 
 foot:2, 
 }); 
 this.timer = setTimeout( 
 () => { 
  pageNum ++; 
  this._fetchListData(); 
 },500); 
} 

這里需要注意一下幾點(diǎn)

1、第一屏的時(shí)候可能也會(huì)觸發(fā)_endReached方法,所以需要判斷foot為非 0(即加載中和已加載完畢)時(shí),直接return

2、上拉時(shí),觸發(fā)_endReached方法,可能server端接口響應(yīng)很快,幾乎看不到菊花效果,特地加了個(gè)500毫秒的等待

9、卸載Timer

componentWillUnmount() { 
// 如果存在this.timer,則使用clearTimeout清空。 
// 如果你使用多個(gè)timer,那么用多個(gè)變量,或者用個(gè)數(shù)組來(lái)保存引用,然后逐個(gè)clear 
 this.timer && clearTimeout(this.timer); 
} 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

邮箱| 政和县| 珲春市| 泊头市| 龙海市| 枣阳市| 浦县| 鹤峰县| 枝江市| 永新县| 阳新县| 吉安市| 庄河市| 玛多县| 武功县| 黎平县| 巫山县| 桃园县| 乌鲁木齐县| 罗源县| 屏南县| 靖远县| 湄潭县| 界首市| 彩票| 隆回县| 朔州市| 平乐县| 海兴县| 永兴县| 涿鹿县| 柳江县| 舟曲县| 监利县| 兴仁县| 稷山县| 徐闻县| 苗栗市| 东至县| 新密市| 贵阳市|