android使用PullToRefresh框架實(shí)現(xiàn)ListView下拉刷新上拉加載更多
本文實(shí)例為大家分享了Android實(shí)現(xiàn)ListView下拉刷新上拉加載更多的具體代碼,供大家參考,具體內(nèi)容如下
其實(shí)谷歌官方目前已經(jīng)推出ListView下拉刷新框架SwipeRefreshLayout,想了解的朋友可以點(diǎn)擊 android使用SwipeRefreshLayout實(shí)現(xiàn)ListView下拉刷新上拉加載 了解一下;
大家不難發(fā)現(xiàn)當(dāng)你使用SwipeRefreshLayout下拉的時(shí)候布局文件不會跟著手勢往下滑,而且想要更改這個(gè)缺陷好像非常不容易。
雖然SwipeRefreshLayout非常簡單易懂,但是需求需要下拉刷新的時(shí)候跟著手勢下滑就不能用SwipeRefreshLayout了;
上面圖片效果使用的是PullToRefresh框架,在我的工程里面沒有導(dǎo)入類庫和jar包,而是把下拉刷新功能直接抽取出來使用;
當(dāng)下拉的時(shí)候回調(diào)監(jiān)聽,在抽取完下拉刷新功能的基礎(chǔ)上實(shí)現(xiàn)上拉加載更多功能實(shí)現(xiàn)也非常簡單,所以順手寫上了;
我是從github上下載的Android-PullToRefresh-master框架,在library中抽取的;
首先需要復(fù)制的類大概有十個(gè)左右:
然后跟進(jìn)報(bào)錯(cuò)查看需要什么文件就復(fù)制什么文件;把錯(cuò)誤搞定之后首先來看下布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 我們添加了一個(gè)屬性:ptr:ptrMode="both" ,意思:上拉和下拉都支持。 可選值為:disabled(禁用下拉刷新),pullFromStart(僅支持下拉刷新), pullFromEnd(僅支持上拉刷新),both(二者都支持),manualOnly(只允許手動觸發(fā)) --> <!-- ptr:ptrAnimationStyle="rotate" FlipLoadingLayout為iOS風(fēng)格的箭頭顛倒的刷新動畫 ptr:ptrAnimationStyle="flip" RotateLoadingLayout為android風(fēng)格的圖片旋轉(zhuǎn)動畫 --> <com.ptrflv.www.pulltorefreshlistview.PullToRefreshListView xmlns:ptr="http://schemas.android.com/apk/res-auto" android:id="@+id/pull_to_refresh_listview" android:layout_width="wrap_content" android:layout_height="wrap_content" ptr:ptrMode="both" ptr:ptrAnimationStyle="flip" /> </LinearLayout>
值得注意的是默認(rèn)情況下下拉刷新的執(zhí)行動畫中顯示的文本是英文,這里我們需要手動修改pull_refresh_strings.xml中的內(nèi)容:
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- 上拉刷新 --> <!-- …代表三個(gè)點(diǎn) ... --> <string name="pull_to_refresh_pull_label">向下拉刷新…</string> <string name="pull_to_refresh_release_label">松開更新…</string> <string name="pull_to_refresh_refreshing_label">正在加載…</string> <!-- 下拉加載更多 --> <string name="pull_to_refresh_from_bottom_pull_label">向下拉加載更多…</string> <string name="pull_to_refresh_from_bottom_release_label">松開加載更多…</string> <string name="pull_to_refresh_from_bottom_refreshing_label">正在加載…</string> </resources>
下面是調(diào)用下拉刷新和上下加載更多的代碼:
public class MainActivity extends Activity { private PullToRefreshListView pullToRefreshListView; //adapter的數(shù)據(jù)源 private List<String> numList=new ArrayList<String>(); private ArrayAdapter<String> arrayAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pullToRefreshListView=(PullToRefreshListView) findViewById(R.id.pull_to_refresh_listview); //初始化數(shù)據(jù) for(int x=0;x<18;x++){ numList.add(""+x); } arrayAdapter = new ArrayAdapter<String>(this, R.layout.item_listview,R.id.textview,numList); pullToRefreshListView.setAdapter(arrayAdapter); //設(shè)定刷新監(jiān)聽 pullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() { @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) { String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL); // 顯示最后更新的時(shí)間 refreshView.getLoadingLayoutProxy() .setLastUpdatedLabel(label); //代表下拉刷新 if(refreshView.getHeaderLayout().isShown()){ new Thread(){ public void run() { try { sleep(1000); handler.sendEmptyMessage(99); } catch (InterruptedException e) { e.printStackTrace(); } }; }.start(); } //代表下拉刷新 if(refreshView.getFooterLayout().isShown()){ new Thread(){ public void run() { try { sleep(1000); handler.sendEmptyMessage(98); } catch (InterruptedException e) { e.printStackTrace(); } }; }.start(); } } }); } private Handler handler=new Handler(){ public void handleMessage(android.os.Message msg) { if(msg.what==99){ numList.add(0, "英雄聯(lián)盟"); arrayAdapter.notifyDataSetChanged(); //關(guān)閉刷新的動畫 pullToRefreshListView.onRefreshComplete(); } if(msg.what==98){ numList.add(numList.size(), "魔獸世界"); arrayAdapter.notifyDataSetChanged(); //關(guān)閉刷新的動畫 pullToRefreshListView.onRefreshComplete(); } }; }; }
在判斷上拉刷新和下拉加載的時(shí)候
refreshView.getFooterLayout().isShown()
refreshView.getHeaderLayout().isShown()會報(bào)錯(cuò),因?yàn)镻ullToRefreshBase這兩個(gè)方法默認(rèn)不是共有方法,我們需要手動該更為public
源碼下載
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義ListView實(shí)現(xiàn)下拉刷新上拉加載更多
- android RecycleView實(shí)現(xiàn)下拉刷新和上拉加載
- 解決android viewmodel 數(shù)據(jù)刷新異常的問題
- Android巧用XListView實(shí)現(xiàn)萬能下拉刷新控件
- Android自定義view仿微信刷新旋轉(zhuǎn)小風(fēng)車
- Android自定義控件ListView下拉刷新的代碼
- Android ExpandableListView實(shí)現(xiàn)下拉刷新和加載更多效果
- Android RecyclerView的刷新分頁的實(shí)現(xiàn)
- android使用SwipeRefreshLayout實(shí)現(xiàn)ListView下拉刷新上拉加載
- Android 中RecyclerView頂部刷新實(shí)現(xiàn)詳解
- Android四種方式刷新View的操作方法
相關(guān)文章
Android編程顯示網(wǎng)絡(luò)上的圖片實(shí)例詳解
這篇文章主要介紹了Android編程顯示網(wǎng)絡(luò)上的圖片,結(jié)合實(shí)例形式詳細(xì)分析了Android顯示網(wǎng)絡(luò)圖片的流程與具體操作技巧,需要的朋友可以參考下2016-10-10Android高仿QQ6.0側(cè)滑刪除實(shí)例代碼
先給大家分享一下,側(cè)滑刪除,布局也就是前面一個(gè)item,然后有兩個(gè)隱藏的按鈕(TextView也可以),然后我們可以向左側(cè)滑動,然后顯示出來,然后對delete(刪除鍵)實(shí)現(xiàn)監(jiān)聽,就可以了哈。好了那就來看看代碼怎么實(shí)現(xiàn)的吧2016-02-02Android自定義View實(shí)現(xiàn)直播點(diǎn)贊特效
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)直播點(diǎn)贊特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07Android?app啟動節(jié)點(diǎn)與上報(bào)啟動實(shí)例詳解
系統(tǒng)的啟動過程非常復(fù)雜,下面這篇文章主要給大家介紹了關(guān)于Android?app啟動節(jié)點(diǎn)與上報(bào)啟動的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04Android編程實(shí)現(xiàn)兩個(gè)Activity相互切換而不使用onCreate()的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)兩個(gè)Activity相互切換而不使用onCreate()的方法,結(jié)合實(shí)例形式分析了多個(gè)Activity切換而不重新創(chuàng)建的操作技巧,需要的朋友可以參考下2017-01-01Android NDK開發(fā)的環(huán)境搭建與簡單示例
本文主要介紹Android NDK的知識,這里整理了相關(guān)資料,來說明如何搭建相應(yīng)環(huán)境和簡單實(shí)例,幫助大家理解,有興趣的小伙伴可以參考下2016-09-09