分享Android中pullToRefresh的使用心得
pullToRefresh的導(dǎo)入
首先,點擊new按鈕 -> import Module

然后在 New Module界面選擇已經(jīng)在本地的含有源代碼的pullToRefresh。

打開如下圖所示的open Module Settings 按鈕

點擊app中的Dependencies 中右邊框的"+"按鈕,選擇第三個 ,如下所示


選擇Modules : pullToRefreshLibrary ,點擊OK
然后在build.gradle(Module:app)或者你自己要寫的那個android 程序的根文件夾的build.gradle中加入下面一句話
compile project(':pullToRefreshLibrary')
自此,pullToRefresh已經(jīng)導(dǎo)入成功,可以新建一個pullToRefrenshListView驗證一下。
pullToRefreshListView的基本使用
pullToRefreshListView和ListView的使用基本差的不多,只不過ListView的xml要換成
com.handmark.pulltorefresh.library.PullToRefreshListView
例子如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/refresh_list_view"
ptr:ptrDrawable="@drawable/default_ptr_flip"
ptr:ptrAnimationStyle="flip"
ptr:ptrHeaderBackground="#383838"
ptr:ptrHeaderTextColor="#FFFFFF" >
</com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout>
上面的例子中pullToRefreshListView中多了幾個屬性 分別以ptr開頭,這是指定pullToRefreshListView在刷新的時候出現(xiàn)的特效,比如第一個是指定刷新時顯示的圖片,第二個是指定刷新的圖片以何種方式顯示出來,第三個是指定刷新時頭部的背景,第四個是指定刷新時頭部字體的顏色。
以上這些都可以在代碼中設(shè)置。
ListView中每個item的xml還是不變的,adapter的使用和寫法也是不變的,需要改變的只有設(shè)定刷新事件。
接下來在代碼中設(shè)定pullToRefreshListView的一些基本屬性和事件。
步驟一 綁定控件,設(shè)置屬性
綁定控件代碼如下:
private PullToRefreshListView listview; listview = (PullToRefreshListView) findViewById(R.id.refresh_list_view);
設(shè)置刷新時顯示的刷新狀態(tài)
//對pullToListView綁定adapter
listview.setAdapter(adapter);
/*設(shè)置pullToRefreshListView的刷新模式,BOTH代表支持上拉和下拉,PULL_FROM_END代表上拉,PULL_FROM_START代表下拉 */
listview.setMode(PullToRefreshBase.Mode.BOTH);
initRefreshListView();
initRefreshListView方法設(shè)置刷新顯示的狀態(tài)
public void initRefreshListView() {
ILoadingLayout Labels = listview.getLoadingLayoutProxy(true, true);
Labels.setPullLabel("快點拉");
Labels.setRefreshingLabel("正在拉");
Labels.setReleaseLabel("放開刷新");
}
這里通過getLoadingLayoutProxy 方法來指定上拉和下拉時顯示的狀態(tài)的區(qū)別,第一個true 代表下來狀態(tài) ,第二個true 代表上拉的狀態(tài) 。如果想?yún)^(qū)分上拉和下拉狀態(tài)的不同,可以分別設(shè)置getLoadingLayoutProxy ,例子如下:
public void initRefreshListView(){
ILoadingLayout startLabels = pullToRefresh
.getLoadingLayoutProxy(true, false);
startLabels.setPullLabel("下拉刷新");
startLabels.setRefreshingLabel("正在拉");
startLabels.setReleaseLabel("放開刷新");
ILoadingLayout endLabels = pullToRefresh.getLoadingLayoutProxy(
false, true);
endLabels.setPullLabel("上拉刷新");
endLabels.setRefreshingLabel("正在載入...");
endLabels.setReleaseLabel("放開刷新...");
這樣pullToRefreshListView刷新時狀態(tài)就設(shè)定好了。
步驟二 pullToRefreshListView監(jiān)聽事件的設(shè)置
這里主要設(shè)置setOnRefreshListener 事件,根據(jù)剛才設(shè)置的不同的刷新模式,在里面寫的匿名內(nèi)部類也不一樣。
規(guī)則如下:
如果Mode設(shè)置成Mode.BOTH,需要設(shè)置刷新Listener為OnRefreshListener2,并實現(xiàn)onPullDownToRefresh()、onPullUpToRefresh()兩個方法。
如果Mode設(shè)置成Mode.PULL_FROM_START或Mode.PULL_FROM_END,需要設(shè)置刷新Listener為OnRefreshListener,同時實現(xiàn)onRefresh()方法。
當然也可以設(shè)置為OnRefreshListener2,但是Mode.PULL_FROM_START的時候只調(diào)用onPullDownToRefresh()方法,Mode.PULL_FROM_END的時候只調(diào)用onPullUpToRefresh()方法.
這樣在進入該Activity時候,手動上拉和下拉就會實現(xiàn)刷新和加載。
如果想剛進入Activity的時候就執(zhí)行加載,則要調(diào)用如下方法
listview.setRefreshing();
接下來只需要在onPullDownToRefresh和onPullUpToRefresh 編寫要獲取listview新數(shù)據(jù)的方法。
我這里的例子如下:
listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
adapter.addToTop();
new FinishRefresh().execute();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
adapter.addToBottom();
new FinishRefresh().execute();
}
});
我這里在自定義的adapter中寫了2個新方法 addToTop 和addToBottom 分別在頭部加入數(shù)據(jù)和在尾部加入數(shù)據(jù)
方法如下:
private void addToTop() {
for (int i = 0; i < 2; i++) {
Item item = new Item();
item.setText("在頭部加入第" + i + "數(shù)據(jù)");
item.setImageid(R.mipmap.ic_launcher);
listItems.add(i, item);
}
}
private void addToBottom() {
for (int i = 0; i < 2; i++) {
Item item = new Item();
item.setText("在尾部加入第" + i + "數(shù)據(jù)");
item.setImageid(R.mipmap.ic_launcher);
listItems.add(item);
}
}
這里并沒有考慮去重的問題,就是每次刷新結(jié)束后會顯示出刷新的結(jié)果,當再次刷新后,又會執(zhí)行和上次一樣的結(jié)果,實際上,這是不符合邏輯的,當?shù)诙卧谒⑿碌臅r候應(yīng)該進行判斷,如果數(shù)據(jù)一樣就不把數(shù)據(jù)加入到list當中。
接下來 new FinishRefresh().execute(); 是這里我比較疑惑的一個固定寫法,在這個com.handmark.pulltorefresh.library.PullToRefreshListView 框架下,執(zhí)行onRefreshComplete();方法必須在異步下執(zhí)行,不能和主進程一起執(zhí)行,如果直接在下拉,上拉監(jiān)聽方法中寫入onRefreshComplete(); 則在實際刷新中刷新狀態(tài)的顯示header是不會收回去的,換句話說 刷新一直不會完成。
所以要在繼承AsyncTask的類下調(diào)用onRefreshComplete();
private class FinishRefresh extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
listview.onRefreshComplete();
adapter.notifyDataSetChanged();
}
}
至此,pullToRefreshListview就實現(xiàn)了簡單的上拉,下拉使用
步驟三 pullToRefresListView 的其他監(jiān)聽方法
關(guān)于步驟三今天時間有限,先給大家分享到這里,后續(xù)持續(xù)更新。
- Android scrollToTop實現(xiàn)點擊回到頂部(兼容PullTorefreshScrollview)
- android使用Ultra-PullToRefresh實現(xiàn)下拉刷新自定義代碼
- Android使用PullToRefresh完成ListView下拉刷新和左滑刪除功能
- Android開源項目PullToRefresh下拉刷新功能詳解
- Android下拉刷新控件PullToRefresh實例解析
- Android使用PullToRefresh實現(xiàn)上拉加載和下拉刷新效果的代碼
- Android實現(xiàn)簡單的下拉刷新pulltorefresh
- Android程序開發(fā)之使用PullToRefresh實現(xiàn)下拉刷新和上拉加載
- Android PullToRefreshLayout下拉刷新控件的終結(jié)者
- android使用PullToRefresh框架實現(xiàn)ListView下拉刷新上拉加載更多
相關(guān)文章
Android 新聞界面模擬ListView和ViewPager的應(yīng)用
本文主要介紹 Android ListView和ViewPager的應(yīng)用,這里模擬了新聞界面及實現(xiàn)示例代碼,有需要的小伙伴可以參考下2016-09-09
通過Html網(wǎng)頁調(diào)用本地安卓(android)app程序代碼
如何使用html網(wǎng)頁和本地app進行傳遞數(shù)據(jù)呢?經(jīng)過研究,發(fā)現(xiàn)還是有方法的,總結(jié)了一下,大致有一下幾種方式2013-11-11
Android數(shù)據(jù)庫SD卡創(chuàng)建和圖片存取操作
這篇文章主要介紹了Android數(shù)據(jù)庫SD卡創(chuàng)建和圖片存取操作的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android Studio3.6設(shè)置Gradle Offline Mode的方法
這篇文章主要介紹了Android Studio3.6設(shè)置Gradle Offline Mode的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Android 8.0升級不跳轉(zhuǎn)應(yīng)用安裝頁面的解決方法
這篇文章主要為大家詳細介紹了Android 8.0升級不跳轉(zhuǎn)應(yīng)用安裝頁面的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06
Android代碼實現(xiàn)AdapterViews和RecyclerView無限滾動
這篇文章主要為大家詳細介紹了Android代碼實現(xiàn)AdapterViews和RecyclerView無限滾動的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07
Android實現(xiàn)Activities之間進行數(shù)據(jù)傳遞的方法
這篇文章主要介紹了Android實現(xiàn)Activities之間進行數(shù)據(jù)傳遞的方法,涉及Android中Activities的使用技巧,需要的朋友可以參考下2015-04-04
Android簡單實現(xiàn) 緩存數(shù)據(jù)
這篇文章主要介紹了Android簡單實現(xiàn) 緩存數(shù)據(jù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11

