這篇文章主要介紹android如何使用PullToRefresh框架實(shí)現(xiàn)ListView下拉刷新上拉加載更多,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)為客戶提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、程序、域名、空間一條龍服務(wù),提供基于WEB的系統(tǒng)開發(fā). 服務(wù)項(xiàng)目涵蓋了網(wǎng)頁設(shè)計(jì)、網(wǎng)站程序開發(fā)、WEB系統(tǒng)開發(fā)、微信二次開發(fā)、移動(dòng)網(wǎng)站建設(shè)等網(wǎng)站方面業(yè)務(wù)。
具體內(nèi)容如下
其實(shí)谷歌官方目前已經(jīng)推出ListView下拉刷新框架SwipeRefreshLayout,想了解的朋友可以點(diǎn)擊 android使用SwipeRefreshLayout實(shí)現(xiàn)ListView下拉刷新上拉加載 了解一下;
大家不難發(fā)現(xiàn)當(dāng)你使用SwipeRefreshLayout下拉的時(shí)候布局文件不會(huì)跟著手勢(shì)往下滑,而且想要更改這個(gè)缺陷好像非常不容易。
雖然SwipeRefreshLayout非常簡(jiǎn)單易懂,但是需求需要下拉刷新的時(shí)候跟著手勢(shì)下滑就不能用SwipeRefreshLayout了;
上面圖片效果使用的是PullToRefresh框架,在我的工程里面沒有導(dǎo)入類庫和jar包,而是把下拉刷新功能直接抽取出來使用;
當(dāng)下拉的時(shí)候回調(diào)監(jiān)聽,在抽取完下拉刷新功能的基礎(chǔ)上實(shí)現(xiàn)上拉加載更多功能實(shí)現(xiàn)也非常簡(jiǎ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(只允許手動(dòng)觸發(fā)) --> <!-- ptr:ptrAnimationStyle="rotate" FlipLoadingLayout為iOS風(fēng)格的箭頭顛倒的刷新動(dòng)畫 ptr:ptrAnimationStyle="flip" RotateLoadingLayout為android風(fēng)格的圖片旋轉(zhuǎn)動(dòng)畫 --> <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í)行動(dòng)畫中顯示的文本是英文,這里我們需要手動(dòng)修改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)閉刷新的動(dòng)畫 pullToRefreshListView.onRefreshComplete(); } if(msg.what==98){ numList.add(numList.size(), "魔獸世界"); arrayAdapter.notifyDataSetChanged(); //關(guān)閉刷新的動(dòng)畫 pullToRefreshListView.onRefreshComplete(); } }; }; }
在判斷上拉刷新和下拉加載的時(shí)候
refreshView.getFooterLayout().isShown()
refreshView.getHeaderLayout().isShown()會(huì)報(bào)錯(cuò),因?yàn)镻ullToRefreshBase這兩個(gè)方法默認(rèn)不是共有方法,我們需要手動(dòng)該更為public
以上是“android如何使用PullToRefresh框架實(shí)現(xiàn)ListView下拉刷新上拉加載更多”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前標(biāo)題:android如何使用PullToRefresh框架實(shí)現(xiàn)ListView下拉刷新上拉加載更多
分享路徑:http://www.rwnh.cn/article16/jddpdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、用戶體驗(yàn)、搜索引擎優(yōu)化、網(wǎng)站營銷、小程序開發(fā)、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)