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

Android程序開發(fā)之使用PullToRefresh實(shí)現(xiàn)下拉刷新和上拉加載

 更新時(shí)間:2016年07月08日 11:50:59   作者:李狗蛋52635  
這篇文章主要介紹了Android程序開發(fā)之使用PullToRefresh實(shí)現(xiàn)下拉刷新和上拉加載的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下

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

1.ListView

2.ExpandableListView

3.GridView

4.WebView

等多種常用的需要刷新的View類型,而且使用起來也十分方便。
(下載地址:https://github.com/chrisbanes/Android-PullToRefresh

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

一、廢話少說,下拉刷新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)行簡單的設(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ì)收起來呢,且往下看。

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

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

一般來說我們會(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來判斷,這樣并不嚴(yán)謹(jǐn),我依靠是header還是footer處于可見狀態(tài)來區(qū)分下拉和上拉,如果是下拉,那header一定是可見的;反之,footer一定是可見的。

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

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

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

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

在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實(shí)現(xiàn)了下拉刷新和上拉加載,LOL,希望多多少少能幫到大家。

近來發(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也沒有作用。

最后放上一張效果圖

以上所述是小編給大家介紹的使用PullToRefresh實(shí)現(xiàn)下拉刷新和上拉加載,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論