欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android使用PullToRefresh實(shí)現(xiàn)下拉刷新和上拉加載

 更新時(shí)間:2016年12月23日 10:22:36   作者:李狗蛋52635  
本篇文章主要介紹了android使用PullToRefresh實(shí)現(xiàn)下拉刷新和上拉加載,具有一定的參考價(jià)值,有興趣的可以了解一下。

PullToRefresh是一套實(shí)現(xiàn)非常好的下拉刷新庫(kù),它支持:

1.ListView

2.ExpandableListView

3.GridView

4.WebView

等多種常用的需要刷新的View類型,而且使用起來(lái)也十分方便。

demo實(shí)例下載

下載完成,將它導(dǎo)入到eclipse中,作為一個(gè)library導(dǎo)入到你的工程中就好了。

一、廢話少說(shuō),下拉刷新Go。

 1.在你的布局文件中加上你想用的View就好了,比如這兒我想用一個(gè)支持下拉 刷新的ExpandableListView

<com.handmark.pulltorefresh.library.PullToRefreshExpandableListView 
  android:id="@+id/expand_list" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" /> 

2. 在你的Activity代碼中進(jìn)行簡(jiǎn)單的設(shè)置:

mExpandList = (PullToRefreshExpandableListView) rootView.findViewById(R.id.expand_list); 
mExpandList.getRefreshableView().setGroupIndicator(null); 
mExpandList.getRefreshableView().setDivider(null); 
mExpandList.getRefreshableView().setSelector(android.R.color.transparent); 
mExpandList.getRefreshableView().setOnGroupClickListener(this); 
mExpandList.setOnRefreshListener(this); 

第一行是找到這個(gè)View,最后一行是為它加上刷新的監(jiān)聽器,中間的幾行是我對(duì)ExpandableListView進(jìn)行一些設(shè)置。

這樣其實(shí)就已經(jīng)可以下拉刷新了,但刷新時(shí)需要運(yùn)行的代碼寫在哪呢,還有為什么下拉不會(huì)收起來(lái)呢,且往下看。

3.下拉刷新時(shí)執(zhí)行的方法onRefresh()

@Override 
public void onRefresh(PullToRefreshBase<ExpandableListView> refreshView) { 
  if (!isRefreshing) { 
    isRefreshing = true; 
    updateList(true); 
  } else { 
    mExpandList.onRefreshComplete(); 
  } 
} 

一般來(lái)說(shuō)我們會(huì)開另一個(gè)線程去獲取數(shù)據(jù),所以這兒會(huì)加上一個(gè)判斷,如果已經(jīng)在獲取數(shù)據(jù)了,就onRefreshComplete(),就是將下拉收起;否則就去開新線程取數(shù)據(jù),取完記得也要onRefreshComplete()哦!

二、上拉加載

如果你不想再費(fèi)時(shí)間去自己寫一個(gè)上拉加載,不妨試一下PullToRefresh自帶的上拉效果哦!

PullToRefresh本身支持下拉刷新和上拉刷新,所以我們只需要將上拉刷新改成上拉加載就行了。

1.設(shè)置Mode

// set mode to BOTH 
mExpandList.setMode(Mode.BOTH); 
mExpandList.getLoadingLayoutProxy(false, true).setPullLabel(getString(R.string.pull_to_load)); 
mExpandList.getLoadingLayoutProxy(false, true).setRefreshingLabel(getString(R.string.loading)); 
mExpandList.getLoadingLayoutProxy(false, true).setReleaseLabel(getString(R.string.release_to_load)); 

Mode設(shè)置為Mode.BOTH后,下拉和上拉都會(huì)執(zhí)行onRefresh()中的方法了。

因?yàn)榻缑嫔线?,我們要顯示“下拉刷新”,下邊我們要顯示“上拉加載”,所以后三行就是改變下邊部分的文字,getLoadingLayoutProxy(false, true)方法大家可以自己感受一下。

2.怎么區(qū)分下拉/上拉

網(wǎng)上有的同學(xué)是用onScrollListener來(lái)判斷,這樣并不嚴(yán)謹(jǐn),我依靠是header還是footer處于可見狀態(tài)來(lái)區(qū)分下拉和上拉,如果是下拉,那header一定是可見的;反之,footer一定是可見的。

但是PullToRefreshExpandableListView并沒(méi)有提供這樣的接口,那我們就來(lái)小改一下我們引入的工程吧,很簡(jiǎn)單:

找到包“com.handmark.pulltorefresh.library”下的PullToRefreshAdapterViewBase.Java這個(gè)類,加入兩個(gè)新接口:

public boolean isHeaderShown() { 
  return getHeaderLayout().isShown(); 
} 
 
public boolean isFooterShown() { 
  return getFooterLayout().isShown(); 
} 

這樣就行了哦,重新編譯一下這個(gè)工程,和你自己的工程。

在onRefresh()中這樣來(lái)用:

@Override 
public void onRefresh(PullToRefreshBase<ExpandableListView> refreshView) { 
  if (!isRefreshing) { 
    isRefreshing = true; 
    if (mExpandList.isHeaderShown()) { 
      Utils.LOGD("pull-to-refresh"); 
      refreshOnlineStatus(true); 
    } else if (mExpandList.isFooterShown()) { 
      Utils.LOGD("pull-to-load-more"); 
      loadNextPage(); 
    } 
  } else { 
    mExpandList.onRefreshComplete(); 
  } 
} 

很簡(jiǎn)單吧,這樣我們就YD地使用PullToRefresh實(shí)現(xiàn)了下拉刷新和上拉加載,LOL,希望多多少少能幫到大家。

=================================================================

更新于2014-07-01

近來(lái)發(fā)現(xiàn):

1.實(shí)現(xiàn)上拉監(jiān)聽,只需要實(shí)現(xiàn)OnRefreshListener2就可以了,同時(shí)別忘記setMode(Mode.BOTH) 哦!

2.PullToRefreshListView在使用上有一個(gè)BUG,在你的xml layout中,不能一開始將它的visiablity設(shè)置為GONE,否則,在代碼中設(shè)置visiablity為VISIABLE也沒(méi)有作用。

最后放上一張效果圖

demo:下載

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

相關(guān)文章

最新評(píng)論