Android開源項目PullToRefresh下拉刷新功能詳解
先看看效果圖:

開源項地址:https://github.com/chrisbanes/Android-PullToRefresh
下拉刷新這個功能我們都比較常見了,今天介紹的就是這個功能的實現(xiàn)。我將按照這個開源庫的范例來一點一點介紹,今天是介紹比較常見的PullToRefreshListView,是讓listView有下拉刷新功能。
1.下載項目包,將library包導入即可,其他的包暫時不用
2.分析源碼,看我們可以設置的有哪些
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="PullToRefresh"> <!-- A drawable to use as the background of the Refreshable View --> <!-- 設置刷新view的背景 --> <attr name="ptrRefreshableViewBackground" format="reference|color" /> <!-- A drawable to use as the background of the Header and Footer Loading Views --> <!-- 設置頭部view的背景 --> <attr name="ptrHeaderBackground" format="reference|color" /> <!-- Text Color of the Header and Footer Loading Views --> <!-- 設置頭部/底部文字的顏色 --> <attr name="ptrHeaderTextColor" format="reference|color" /> <!-- Text Color of the Header and Footer Loading Views Sub Header --> <!-- 設置頭部/底部副標題的文字顏色 --> <attr name="ptrHeaderSubTextColor" format="reference|color" /> <!-- Mode of Pull-to-Refresh that should be used --> <!-- 設置下拉刷新的模式,有多重方式可選。無刷新功能,從頂部刷新,從底部刷新,二者都有,只允許手動刷新 --> <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 --> <!-- 這兩個屬性不推薦了,用上面的代替即可 --> <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. --> <!-- 頂部指示箭頭的圖片,設置后會覆蓋ptrDrawable中頂部的設置 --> <attr name="ptrDrawableStart" format="reference" /> <!-- Drawable to use as Loading Indicator in the Fooer View. Overrides value set in ptrDrawable. --> <!-- 底部指示箭頭的圖片,設置后會覆蓋ptrDrawable中底部的設置 --> <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 --> <!-- 設置文字的基本字體 --> <attr name="ptrHeaderTextAppearance" format="reference" /> <!-- Base text color, typeface, size, and style for Header and Footer Loading Views Sub Header --> <!-- 設置副標題的基本字體 --> <attr name="ptrSubHeaderTextAppearance" format="reference" /> <!-- Style of Animation should be used displayed when pulling. --> <!-- 設置下拉時標識圖的動畫,默認為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 --> <!-- 設置刷新時是否允許滾動,一般為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. --> <!-- 當設置rotate時,可以用這個來設置刷新時旋轉的圖片 --> <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>
看到有這么多可以設置的屬性,別以為真的就可以定制了。真正要定制還得到layout中改變刷新布局

3.開始用它建立自己的工程
設置布局文件
就是插入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.找到這個控件,并且設置監(jiān)聽器
這里面用到了一個日期的工具類,其實就是設置上次下拉的時間的。此外在下拉后會觸發(fā)一個異步任務
/**
* 設置下拉刷新的listview的動作
*/
private void initPTRListView() {
mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
//設置拉動監(jiān)聽器
mPullRefreshListView.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);
// 更新顯示的label
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
// 開始執(zhí)行異步任務,傳入適配器來進行數(shù)據(jù)改變
new GetDataTask(mPullRefreshListView, mAdapter,mListItems).execute();
}
});
// 添加滑動到底部的監(jiān)聽器
mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
Toast.makeText(getApplication(), "已經(jīng)到底了", Toast.LENGTH_SHORT).show();
}
});
//mPullRefreshListView.isScrollingWhileRefreshingEnabled();//看刷新時是否允許滑動
//在刷新時允許繼續(xù)滑動
mPullRefreshListView.setScrollingWhileRefreshingEnabled(true);
//mPullRefreshListView.getMode();//得到模式
//上下都可以刷新的模式。這里有兩個選擇:Mode.PULL_FROM_START,Mode.BOTH,PULL_FROM_END
mPullRefreshListView.setMode(Mode.BOTH);
/**
* 設置反饋音效
*/
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.從上面的那個控件中,得到它包含的listView,并且設置適配器
//普通的listview對象 private ListView actualListView; //添加一個鏈表數(shù)組,來存放string數(shù)組,這樣就可以動態(tài)增加string數(shù)組中的內(nèi)容了 private LinkedList<String> mListItems; //給listview添加一個普通的適配器 private ArrayAdapter<String> mAdapter;
這里用到了一個LinkedList的對象,這個是一個類似于ArrayList的鏈表數(shù)組,比較方便在開頭和末尾添加String
/**
* 設置listview的適配器
*/
private void initListView() {
//通過getRefreshableView()來得到一個listview對象
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.寫一個異步任務,來模仿從網(wǎng)絡加載數(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ǎng)絡中的數(shù)據(jù),進行更新
* @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 自動生成的構造函數(shù)存根
mPullRefreshListView = listView;
mAdapter = adapter;
mListItems = listItems;
}
@Override
protected Void doInBackground(Void... params) {
//模擬請求
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO 自動生成的方法存根
super.onPostExecute(result);
//得到當前的模式
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 {
//一個可以下拉刷新的listView對象
private PullToRefreshListView mPullRefreshListView;
//普通的listview對象
private ListView actualListView;
//添加一個鏈表數(shù)組,來存放string數(shù)組,這樣就可以動態(tài)增加string數(shù)組中的內(nèi)容了
private LinkedList<String> mListItems;
//給listview添加一個普通的適配器
private ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//一打開應用就自動刷新,下面語句可以寫到刷新按鈕里面
mPullRefreshListView.setRefreshing(true);
//new GetDataTask(mPullRefreshListView, mAdapter, mListItems).execute();
//mPullRefreshListView.setRefreshing(false);
}
private void initView() {
initPTRListView();
initListView();
}
/**
* 設置下拉刷新的listview的動作
*/
private void initPTRListView() {
mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
//設置拉動監(jiān)聽器
mPullRefreshListView.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);
// 更新顯示的label
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
// 開始執(zhí)行異步任務,傳入適配器來進行數(shù)據(jù)改變
new GetDataTask(mPullRefreshListView, mAdapter,mListItems).execute();
}
});
// 添加滑動到底部的監(jiān)聽器
mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
Toast.makeText(getApplication(), "已經(jīng)到底了", Toast.LENGTH_SHORT).show();
}
});
//mPullRefreshListView.isScrollingWhileRefreshingEnabled();//看刷新時是否允許滑動
//在刷新時允許繼續(xù)滑動
mPullRefreshListView.setScrollingWhileRefreshingEnabled(true);
//mPullRefreshListView.getMode();//得到模式
//上下都可以刷新的模式。這里有兩個選擇:Mode.PULL_FROM_START,Mode.BOTH,PULL_FROM_END
mPullRefreshListView.setMode(Mode.BOTH);
/**
* 設置反饋音效
*/
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);
}
/**
* 設置listview的適配器
*/
private void initListView() {
//通過getRefreshableView()來得到一個listview對象
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
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- android使用PullToRefresh框架實現(xiàn)ListView下拉刷新上拉加載更多
- android使用Ultra-PullToRefresh實現(xiàn)下拉刷新自定義代碼
- android使用PullToRefresh實現(xiàn)下拉刷新和上拉加載
- Android使用PullToRefresh完成ListView下拉刷新和左滑刪除功能
- Android開源項目PullToRefresh下拉刷新功能詳解2
- Android下拉刷新控件PullToRefresh實例解析
- Android使用PullToRefresh實現(xiàn)上拉加載和下拉刷新效果的代碼
- Android實現(xiàn)簡單的下拉刷新pulltorefresh
- Android程序開發(fā)之使用PullToRefresh實現(xiàn)下拉刷新和上拉加載
- Android PullToRefreshLayout下拉刷新控件的終結者
- Android帶刷新時間顯示的PullToRefresh上下拉刷新
相關文章
Android編程記錄ListView標記行狀態(tài)的方法
這篇文章主要介紹了Android編程記錄ListView標記行狀態(tài)的方法,結合實例分析了ListView標記的相關實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
Android實現(xiàn)橫向無限循環(huán)滾動的單行彈幕效果
這篇文章主要為大家詳細介紹了Android實現(xiàn)橫向無限循環(huán)滾動的單行彈幕效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06
Android?Jetpack庫重要組件WorkManager的使用
WorkManager是Android?Jetpack的一個強大的組件,用于處理后臺耗時任務。后臺任務可以是一次性的,也可以是重復的,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08
android studio 3.4配置Android -jni 開發(fā)基礎的教程詳解
這篇文章主要介紹了android studio 3.4配置Android -jni 開發(fā)基礎,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
Android中利用xml文件布局修改Helloworld程序
這篇文章主要介紹了Android中利用xml文件布局修改Helloworld程序 的相關資料,需要的朋友可以參考下2016-07-07
Android實現(xiàn)SwipeRefreshLayout首次進入自動刷新
這篇文章主要為大家詳細介紹了Android實現(xiàn)SwipeRefreshLayout首次進入自動刷新,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01

