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í)候布局文件不會(huì)跟著手勢(shì)往下滑,而且想要更改這個(gè)缺陷好像非常不容易。
雖然SwipeRefreshLayout非常簡(jiǎn)單易懂,但是需求需要下拉刷新的時(shí)候跟著手勢(shì)下滑就不能用SwipeRefreshLayout了;
上面圖片效果使用的是PullToRefresh框架,在我的工程里面沒(méi)有導(dǎo)入類(lèi)庫(kù)和jar包,而是把下拉刷新功能直接抽取出來(lái)使用;
當(dāng)下拉的時(shí)候回調(diào)監(jiān)聽(tīng),在抽取完下拉刷新功能的基礎(chǔ)上實(shí)現(xiàn)上拉加載更多功能實(shí)現(xiàn)也非常簡(jiǎn)單,所以順手寫(xiě)上了;
我是從github上下載的Android-PullToRefresh-master框架,在library中抽取的;
首先需要復(fù)制的類(lèi)大概有十個(gè)左右:

然后跟進(jìn)報(bào)錯(cuò)查看需要什么文件就復(fù)制什么文件;把錯(cuò)誤搞定之后首先來(lái)看下布局:
<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)畫(huà) ptr:ptrAnimationStyle="flip" RotateLoadingLayout為android風(fēng)格的圖片旋轉(zhuǎn)動(dòng)畫(huà) --> <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)畫(huà)中顯示的文本是英文,這里我們需要手動(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">松開(kāi)更新…</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">松開(kāi)加載更多…</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)聽(tīng)
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)畫(huà)
pullToRefreshListView.onRefreshComplete();
}
if(msg.what==98){
numList.add(numList.size(), "魔獸世界");
arrayAdapter.notifyDataSetChanged();
//關(guān)閉刷新的動(dòng)畫(huà)
pullToRefreshListView.onRefreshComplete();
}
};
};
}
在判斷上拉刷新和下拉加載的時(shí)候
refreshView.getFooterLayout().isShown()
refreshView.getHeaderLayout().isShown()會(huì)報(bào)錯(cuò),因?yàn)镻ullToRefreshBase這兩個(gè)方法默認(rèn)不是共有方法,我們需要手動(dòng)該更為public
源碼下載
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義ListView實(shí)現(xiàn)下拉刷新上拉加載更多
- android RecycleView實(shí)現(xiàn)下拉刷新和上拉加載
- 解決android viewmodel 數(shù)據(jù)刷新異常的問(wèn)題
- Android巧用XListView實(shí)現(xiàn)萬(wàn)能下拉刷新控件
- Android自定義view仿微信刷新旋轉(zhuǎn)小風(fēng)車(chē)
- Android自定義控件ListView下拉刷新的代碼
- Android ExpandableListView實(shí)現(xiàn)下拉刷新和加載更多效果
- Android RecyclerView的刷新分頁(yè)的實(shí)現(xiàn)
- android使用SwipeRefreshLayout實(shí)現(xiàn)ListView下拉刷新上拉加載
- Android 中RecyclerView頂部刷新實(shí)現(xiàn)詳解
- Android四種方式刷新View的操作方法
相關(guān)文章
Android懸浮窗的實(shí)現(xiàn)(易錯(cuò)點(diǎn))
現(xiàn)在很多應(yīng)用都使用到懸浮窗,例如微信在視頻的時(shí)候,點(diǎn)擊Home鍵,視頻小窗口仍然會(huì)在屏幕上顯示。下面小編來(lái)實(shí)現(xiàn)一下android 懸浮窗,感興趣的朋友跟隨小編一起看看吧2019-10-10
詳解Flutter WebView與JS互相調(diào)用簡(jiǎn)易指南
這篇文章主要介紹了詳解Flutter WebView與JS互相調(diào)用簡(jiǎn)易指南,分為JS調(diào)用Flutter和Flutter調(diào)用JS,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
Android移動(dòng)應(yīng)用開(kāi)發(fā)指南之六種布局詳解
Android應(yīng)用界面要美觀(guān)好看,就需要運(yùn)用到一定的布局技術(shù),Android布局是不可忽視的,是android應(yīng)用界面開(kāi)發(fā)的重要一環(huán),這篇文章主要給大家介紹了關(guān)于Android移動(dòng)應(yīng)用開(kāi)發(fā)指南之六種布局的相關(guān)資料,需要的朋友可以參考下2022-09-09
安卓Android Context類(lèi)實(shí)例詳解
在開(kāi)發(fā)Android的過(guò)程中,總是能遇見(jiàn)Context類(lèi)或者它的實(shí)例.Context類(lèi)的實(shí)例經(jīng)常被用來(lái)提供“應(yīng)用程序”的引用,下面舉例說(shuō)明Contex類(lèi)實(shí)例詳解2016-07-07
Android編程之Application設(shè)置全局變量及傳值用法實(shí)例分析
這篇文章主要介紹了Android編程之Application設(shè)置全局變量及傳值用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了全局變量及傳值的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12

