Android開源項(xiàng)目PullToRefresh下拉刷新功能詳解
先看看效果圖:
開源項(xiàng)地址:https://github.com/chrisbanes/Android-PullToRefresh
下拉刷新這個(gè)功能我們都比較常見了,今天介紹的就是這個(gè)功能的實(shí)現(xiàn)。我將按照這個(gè)開源庫(kù)的范例來一點(diǎn)一點(diǎn)介紹,今天是介紹比較常見的PullToRefreshListView,是讓listView有下拉刷新功能。
1.下載項(xiàng)目包,將library包導(dǎo)入即可,其他的包暫時(shí)不用
2.分析源碼,看我們可以設(shè)置的有哪些
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="PullToRefresh"> <!-- A drawable to use as the background of the Refreshable View --> <!-- 設(shè)置刷新view的背景 --> <attr name="ptrRefreshableViewBackground" format="reference|color" /> <!-- A drawable to use as the background of the Header and Footer Loading Views --> <!-- 設(shè)置頭部view的背景 --> <attr name="ptrHeaderBackground" format="reference|color" /> <!-- Text Color of the Header and Footer Loading Views --> <!-- 設(shè)置頭部/底部文字的顏色 --> <attr name="ptrHeaderTextColor" format="reference|color" /> <!-- Text Color of the Header and Footer Loading Views Sub Header --> <!-- 設(shè)置頭部/底部副標(biāo)題的文字顏色 --> <attr name="ptrHeaderSubTextColor" format="reference|color" /> <!-- Mode of Pull-to-Refresh that should be used --> <!-- 設(shè)置下拉刷新的模式,有多重方式可選。無刷新功能,從頂部刷新,從底部刷新,二者都有,只允許手動(dòng)刷新 --> <attr name="ptrMode"> <flag name="disabled" value="0x0" /> <flag name="pullFromStart" value="0x1" /> <flag name="pullFromEnd" value="0x2" /> <flag name="both" value="0x3" /> <flag name="manualOnly" value="0x4" /> <!-- These last two are depreacted --> <!-- 這兩個(gè)屬性不推薦了,用上面的代替即可 --> <flag name="pullDownFromTop" value="0x1" /> <flag name="pullUpFromBottom" value="0x2" /> </attr> <!-- Whether the Indicator overlay(s) should be used --> <!-- 是否顯示指示箭頭 --> <attr name="ptrShowIndicator" format="reference|boolean" /> <!-- Drawable to use as Loading Indicator. Changes both Header and Footer. --> <!-- 指示箭頭的圖片 --> <attr name="ptrDrawable" format="reference" /> <!-- Drawable to use as Loading Indicator in the Header View. Overrides value set in ptrDrawable. --> <!-- 頂部指示箭頭的圖片,設(shè)置后會(huì)覆蓋ptrDrawable中頂部的設(shè)置 --> <attr name="ptrDrawableStart" format="reference" /> <!-- Drawable to use as Loading Indicator in the Fooer View. Overrides value set in ptrDrawable. --> <!-- 底部指示箭頭的圖片,設(shè)置后會(huì)覆蓋ptrDrawable中底部的設(shè)置 --> <attr name="ptrDrawableEnd" format="reference" /> <!-- Whether Android's built-in Over Scroll should be utilised for Pull-to-Refresh. --> <attr name="ptrOverScroll" format="reference|boolean" /> <!-- Base text color, typeface, size, and style for Header and Footer Loading Views --> <!-- 設(shè)置文字的基本字體 --> <attr name="ptrHeaderTextAppearance" format="reference" /> <!-- Base text color, typeface, size, and style for Header and Footer Loading Views Sub Header --> <!-- 設(shè)置副標(biāo)題的基本字體 --> <attr name="ptrSubHeaderTextAppearance" format="reference" /> <!-- Style of Animation should be used displayed when pulling. --> <!-- 設(shè)置下拉時(shí)標(biāo)識(shí)圖的動(dòng)畫,默認(rèn)為rotate --> <attr name="ptrAnimationStyle"> <flag name="rotate" value="0x0" /> <flag name="flip" value="0x1" /> </attr> <!-- Whether the user can scroll while the View is Refreshing --> <!-- 設(shè)置刷新時(shí)是否允許滾動(dòng),一般為true --> <attr name="ptrScrollingWhileRefreshingEnabled" format="reference|boolean" /> <!-- Whether PullToRefreshListView has it's extras enabled. This allows the user to be able to scroll while refreshing, and behaves better. It acheives this by adding Header and/or Footer Views to the ListView. --> <!-- 允許在listview中添加頭/尾視圖 --> <attr name="ptrListViewExtrasEnabled" format="reference|boolean" /> <!-- Whether the Drawable should be continually rotated as you pull. This only takes effect when using the 'Rotate' Animation Style. --> <!-- 當(dāng)設(shè)置rotate時(shí),可以用這個(gè)來設(shè)置刷新時(shí)旋轉(zhuǎn)的圖片 --> <attr name="ptrRotateDrawableWhilePulling" format="reference|boolean" /> <!-- BELOW HERE ARE DEPRECEATED. DO NOT USE. --> <attr name="ptrAdapterViewBackground" format="reference|color" /> <attr name="ptrDrawableTop" format="reference" /> <attr name="ptrDrawableBottom" format="reference" /> </declare-styleable> </resources>
看到有這么多可以設(shè)置的屬性,別以為真的就可以定制了。真正要定制還得到layout中改變刷新布局
3.開始用它建立自己的工程
設(shè)置布局文件
就是插入PullToRefreshListView
<RelativeLayout 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" tools:context="${relativePackage}.${activityClass}" android:background="#000000"> <!-- The PullToRefreshListView replaces a standard ListView widget. --> <com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:ptr="http://schemas.android.com/apk/res-auto" android:id="@+id/pull_refresh_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#000000" android:divider="#19000000" android:dividerHeight="4dp" android:fadingEdge="none" android:fastScrollEnabled="false" android:footerDividersEnabled="false" android:headerDividersEnabled="false" android:smoothScrollbar="true" ptr:ptrAnimationStyle="rotate" ptr:ptrHeaderTextColor="#ffffff" ptr:ptrHeaderSubTextColor="#00ffff" ptr:ptrHeaderBackground="@null" ptr:ptrDrawable="@drawable/ic_launcher"/> </RelativeLayout>
開始編寫代碼
1.找到這個(gè)控件,并且設(shè)置監(jiān)聽器
這里面用到了一個(gè)日期的工具類,其實(shí)就是設(shè)置上次下拉的時(shí)間的。此外在下拉后會(huì)觸發(fā)一個(gè)異步任務(wù)
/** * 設(shè)置下拉刷新的listview的動(dòng)作 */ private void initPTRListView() { mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list); //設(shè)置拉動(dòng)監(jiān)聽器 mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() { @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) { //設(shè)置下拉時(shí)顯示的日期和時(shí)間 String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL); // 更新顯示的label refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label); // 開始執(zhí)行異步任務(wù),傳入適配器來進(jìn)行數(shù)據(jù)改變 new GetDataTask(mPullRefreshListView, mAdapter,mListItems).execute(); } }); // 添加滑動(dòng)到底部的監(jiān)聽器 mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() { @Override public void onLastItemVisible() { Toast.makeText(getApplication(), "已經(jīng)到底了", Toast.LENGTH_SHORT).show(); } }); //mPullRefreshListView.isScrollingWhileRefreshingEnabled();//看刷新時(shí)是否允許滑動(dòng) //在刷新時(shí)允許繼續(xù)滑動(dòng) mPullRefreshListView.setScrollingWhileRefreshingEnabled(true); //mPullRefreshListView.getMode();//得到模式 //上下都可以刷新的模式。這里有兩個(gè)選擇:Mode.PULL_FROM_START,Mode.BOTH,PULL_FROM_END mPullRefreshListView.setMode(Mode.BOTH); /** * 設(shè)置反饋音效 */ SoundPullEventListener<ListView> soundListener = new SoundPullEventListener<ListView>(this); soundListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event); soundListener.addSoundEvent(State.RESET, R.raw.reset_sound); soundListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound); mPullRefreshListView.setOnPullEventListener(soundListener); }
2.從上面的那個(gè)控件中,得到它包含的listView,并且設(shè)置適配器
//普通的listview對(duì)象 private ListView actualListView; //添加一個(gè)鏈表數(shù)組,來存放string數(shù)組,這樣就可以動(dòng)態(tài)增加string數(shù)組中的內(nèi)容了 private LinkedList<String> mListItems; //給listview添加一個(gè)普通的適配器 private ArrayAdapter<String> mAdapter;
這里用到了一個(gè)LinkedList的對(duì)象,這個(gè)是一個(gè)類似于ArrayList的鏈表數(shù)組,比較方便在開頭和末尾添加String
/** * 設(shè)置listview的適配器 */ private void initListView() { //通過getRefreshableView()來得到一個(gè)listview對(duì)象 actualListView = mPullRefreshListView.getRefreshableView(); String []data = new String[] {"android","ios","wp","java","c++","c#"}; mListItems = new LinkedList<String>(); //把string數(shù)組中的string添加到鏈表中 mListItems.addAll(Arrays.asList(data)); mAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, mListItems); actualListView.setAdapter(mAdapter); }
3.寫一個(gè)異步任務(wù),來模仿從網(wǎng)絡(luò)加載數(shù)據(jù)
這里要注意的是,加載完后要出發(fā)刷新完成和通知適配器改變的方法
package com.kale.ptrlistviewtest; import java.util.LinkedList; import android.os.AsyncTask; import android.widget.ArrayAdapter; import com.handmark.pulltorefresh.library.PullToRefreshListView; import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode; /** * @author:Jack Tony * @tips :通過異步任務(wù)來加載網(wǎng)絡(luò)中的數(shù)據(jù),進(jìn)行更新 * @date :2014-10-14 */ public class GetDataTask extends AsyncTask<Void, Void, Void>{ private PullToRefreshListView mPullRefreshListView; private ArrayAdapter<String> mAdapter; private LinkedList<String> mListItems; public GetDataTask(PullToRefreshListView listView, ArrayAdapter<String> adapter,LinkedList<String> listItems) { // TODO 自動(dòng)生成的構(gòu)造函數(shù)存根 mPullRefreshListView = listView; mAdapter = adapter; mListItems = listItems; } @Override protected Void doInBackground(Void... params) { //模擬請(qǐng)求 try { Thread.sleep(2000); } catch (InterruptedException e) { } return null; } @Override protected void onPostExecute(Void result) { // TODO 自動(dòng)生成的方法存根 super.onPostExecute(result); //得到當(dāng)前的模式 Mode mode = mPullRefreshListView.getCurrentMode(); if(mode == Mode.PULL_FROM_START) { mListItems.addFirst("這是刷新出來的數(shù)據(jù)"); } else { mListItems.addLast("這是刷新出來的數(shù)據(jù)"); } // 通知數(shù)據(jù)改變了 mAdapter.notifyDataSetChanged(); // 加載完成后停止刷新 mPullRefreshListView.onRefreshComplete(); } }
貼上acitivty中的全部代碼
MainActivity.java
package com.kale.ptrlistviewtest; import java.util.Arrays; import java.util.LinkedList; import android.app.Activity; import android.os.Bundle; import android.text.format.DateUtils; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode; import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener; import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener; import com.handmark.pulltorefresh.library.PullToRefreshBase.State; import com.handmark.pulltorefresh.library.PullToRefreshListView; import com.handmark.pulltorefresh.library.extras.SoundPullEventListener; public class MainActivity extends Activity { //一個(gè)可以下拉刷新的listView對(duì)象 private PullToRefreshListView mPullRefreshListView; //普通的listview對(duì)象 private ListView actualListView; //添加一個(gè)鏈表數(shù)組,來存放string數(shù)組,這樣就可以動(dòng)態(tài)增加string數(shù)組中的內(nèi)容了 private LinkedList<String> mListItems; //給listview添加一個(gè)普通的適配器 private ArrayAdapter<String> mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); //一打開應(yīng)用就自動(dòng)刷新,下面語句可以寫到刷新按鈕里面 mPullRefreshListView.setRefreshing(true); //new GetDataTask(mPullRefreshListView, mAdapter, mListItems).execute(); //mPullRefreshListView.setRefreshing(false); } private void initView() { initPTRListView(); initListView(); } /** * 設(shè)置下拉刷新的listview的動(dòng)作 */ private void initPTRListView() { mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list); //設(shè)置拉動(dòng)監(jiān)聽器 mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() { @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) { //設(shè)置下拉時(shí)顯示的日期和時(shí)間 String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL); // 更新顯示的label refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label); // 開始執(zhí)行異步任務(wù),傳入適配器來進(jìn)行數(shù)據(jù)改變 new GetDataTask(mPullRefreshListView, mAdapter,mListItems).execute(); } }); // 添加滑動(dòng)到底部的監(jiān)聽器 mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() { @Override public void onLastItemVisible() { Toast.makeText(getApplication(), "已經(jīng)到底了", Toast.LENGTH_SHORT).show(); } }); //mPullRefreshListView.isScrollingWhileRefreshingEnabled();//看刷新時(shí)是否允許滑動(dòng) //在刷新時(shí)允許繼續(xù)滑動(dòng) mPullRefreshListView.setScrollingWhileRefreshingEnabled(true); //mPullRefreshListView.getMode();//得到模式 //上下都可以刷新的模式。這里有兩個(gè)選擇:Mode.PULL_FROM_START,Mode.BOTH,PULL_FROM_END mPullRefreshListView.setMode(Mode.BOTH); /** * 設(shè)置反饋音效 */ SoundPullEventListener<ListView> soundListener = new SoundPullEventListener<ListView>(this); soundListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event); soundListener.addSoundEvent(State.RESET, R.raw.reset_sound); soundListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound); mPullRefreshListView.setOnPullEventListener(soundListener); } /** * 設(shè)置listview的適配器 */ private void initListView() { //通過getRefreshableView()來得到一個(gè)listview對(duì)象 actualListView = mPullRefreshListView.getRefreshableView(); String []data = new String[] {"android","ios","wp","java","c++","c#"}; mListItems = new LinkedList<String>(); //把string數(shù)組中的string添加到鏈表中 mListItems.addAll(Arrays.asList(data)); mAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, mListItems); actualListView.setAdapter(mAdapter); } }
源碼下載:http://xiazai.jb51.net/201609/yuanma/AndroidListView(jb51.net).rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android使用PullToRefresh框架實(shí)現(xiàn)ListView下拉刷新上拉加載更多
- android使用Ultra-PullToRefresh實(shí)現(xiàn)下拉刷新自定義代碼
- android使用PullToRefresh實(shí)現(xiàn)下拉刷新和上拉加載
- Android使用PullToRefresh完成ListView下拉刷新和左滑刪除功能
- Android開源項(xiàng)目PullToRefresh下拉刷新功能詳解2
- Android下拉刷新控件PullToRefresh實(shí)例解析
- Android使用PullToRefresh實(shí)現(xiàn)上拉加載和下拉刷新效果的代碼
- Android實(shí)現(xiàn)簡(jiǎn)單的下拉刷新pulltorefresh
- Android程序開發(fā)之使用PullToRefresh實(shí)現(xiàn)下拉刷新和上拉加載
- Android PullToRefreshLayout下拉刷新控件的終結(jié)者
- Android帶刷新時(shí)間顯示的PullToRefresh上下拉刷新
相關(guān)文章
Android獲取SD卡路徑及SDCard內(nèi)存的方法
這篇文章主要介紹了Android獲取SD卡路徑及SDCard內(nèi)存的方法,較為詳細(xì)的分析了Android針對(duì)SD卡操作所涉及的類及其具體函數(shù)功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02Android自定義view利用PathEffect實(shí)現(xiàn)動(dòng)態(tài)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義view利用PathEffect實(shí)現(xiàn)動(dòng)態(tài)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Android實(shí)現(xiàn)水波紋點(diǎn)擊效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)水波紋點(diǎn)擊效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03為Retrofit統(tǒng)一添加post請(qǐng)求的默認(rèn)參數(shù)的方法
這篇文章主要介紹了為Retrofit統(tǒng)一添加post請(qǐng)求的默認(rèn)參數(shù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04Android實(shí)現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單(2)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單第二篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android Studio使用USB真機(jī)調(diào)試詳解
這篇文章主要為大家詳細(xì)介紹了Android Studio使用USB真機(jī)調(diào)試的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android仿QQ分組實(shí)現(xiàn)二級(jí)菜單展示
這篇文章主要為大家詳細(xì)介紹了Android仿QQ分組實(shí)現(xiàn)二級(jí)菜單展示,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09