亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

1.ListView

2.ExpandableListView

3.GridView

4.WebView

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

demo實例下載

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

一、廢話少說,下拉刷新Go。

 1.在你的布局文件中加上你想用的View就好了,比如這兒我想用一個支持下拉 刷新的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)行簡單的設(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); 

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

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

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

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

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

二、上拉加載

如果你不想再費時間去自己寫一個上拉加載,不妨試一下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后,下拉和上拉都會執(zhí)行onRefresh()中的方法了。

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

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

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

但是PullToRefreshExpandableListView并沒有提供這樣的接口,那我們就來小改一下我們引入的工程吧,很簡單:

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

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

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

在onRefresh()中這樣來用:

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

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

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

更新于2014-07-01

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

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

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

最后放上一張效果圖

demo:下載

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

相關(guān)文章

最新評論