Android PullToRefresh (ListView GridView 下拉刷新) 使用详

转载请标明出处:,本文出自:【张鸿洋的博客】

群里一哥们今天聊天偶然提到这个git hub上的控件:pull-to-refresh,有兴趣的看下,例子中的功能极其强大,支持很多控件。本篇博客详细给大家介绍下ListView和GridView利用pull-to-rerfesh 实现下拉刷新和上拉加载更多。

1、ListView下拉刷新快速入门

pull-to-refresh对ListView进行了封装,叫做:PullToRefreshListView,用法和listview没什么区别,下面看demo.

布局文件:

<RelativeLayout xmlns:android=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent" ><com.handmark.pulltorefresh.library.PullToRefreshListViewxmlns:ptr=""android:id="@+id/pull_refresh_list"android:layout_width="fill_parent"android:layout_height="fill_parent"android:cacheColorHint="#00000000"android:divider="#19000000"android:dividerHeight="4dp"android:fadingEdge="none"android:fastScrollEnabled="false"android:footerDividersEnabled="false"android:headerDividersEnabled="false"android:smoothScrollbar="true" ></com.handmark.pulltorefresh.library.PullToRefreshListView></RelativeLayout>声明了一个PullToRefreshListView,里面所有的属性都是ListView的,没有任何其他属性,当然了PullToRefreshListView也提供了很多配置的属性,后面会详细介绍。

Activity的代码:

package com.example.zhy_pulltorefreash_chenyoca;import java.util.LinkedList;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.text.format.DateUtils;import android.widget.ArrayAdapter;import android.widget.ListView;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;import com.handmark.pulltorefresh.library.PullToRefreshListView;public class PullToRefreshListActivity extends Activity{private LinkedList<String> mListItems;/** * 上拉刷新的控件 */private PullToRefreshListView mPullRefreshListView;private ArrayAdapter<String> mAdapter;private int mItemCount = 9;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 得到控件mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);//初始化数据initDatas();//设置适配器mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mListItems);mPullRefreshListView.setAdapter(mAdapter);// 设置监听事件mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>(){@Overridepublic void onRefresh(PullToRefreshBase<ListView> refreshView){String label = DateUtils.formatDateTime(getApplicationContext(),System.currentTimeMillis(),DateUtils.FORMAT_SHOW_TIME| DateUtils.FORMAT_SHOW_DATE| DateUtils.FORMAT_ABBREV_ALL);// 显示最后更新的时间refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);// 模拟加载任务new GetDataTask().execute();}});}private void initDatas(){// 初始化数据和数据源mListItems = new LinkedList<String>();for (int i = 0; i < mItemCount; i++){mListItems.add("" + i);}}private class GetDataTask extends AsyncTask<Void, Void, String>{@Overrideprotected String doInBackground(Void… params){try{Thread.sleep(2000);} catch (InterruptedException e){}return "" + (mItemCount++);}@Overrideprotected void onPostExecute(String result){mListItems.add(result);mAdapter.notifyDataSetChanged();// Call onRefreshComplete when the list has been refreshed.mPullRefreshListView.onRefreshComplete();}}}代码极其简单,得到PullToRefreshListView控件,然后像ListView一样设置数据集。当然了,我们有下拉刷新,所以必须设置下拉刷新的回调:

setOnRefreshListener(new OnRefreshListener<ListView>(){}

我们在回调中模拟了一个异步任务,加载了一个Item。

效果图:

下拉时,执行我们的GetDataTask任务,任务执行完成后在onPostExecute中调用mPullRefreshListView.onRefreshComplete();完成刷新。

是不是分分钟实现下拉刷新。当然了,你可能会有疑问,下拉刷新的指示器上的文字可以自定义吗?那个图片可以换成箭头吗?说好的上拉加载更多呢?后面会一一添加~

2、添加上拉加载更多

如过希望实现上拉加载更多,那么首先需要在布局文件的声明属性中添加一个属性,用于指定目前的下拉模式:

<RelativeLayout xmlns:android=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent" ><com.handmark.pulltorefresh.library.PullToRefreshListViewxmlns:ptr=""android:id="@+id/pull_refresh_list"android:layout_width="fill_parent"android:layout_height="fill_parent"android:cacheColorHint="#00000000"android:divider="#19000000"android:dividerHeight="4dp"android:fadingEdge="none"android:fastScrollEnabled="false"android:footerDividersEnabled="false"android:headerDividersEnabled="false"android:smoothScrollbar="true"ptr:ptrMode="both" ></com.handmark.pulltorefresh.library.PullToRefreshListView></RelativeLayout>我们添加了一个属性:ptr:ptrMode="both" ,意思:上拉和下拉都支持。

可选值为:disabled(禁用下拉刷新),pullFromStart(仅支持下拉刷新),pullFromEnd(仅支持上拉刷新),both(二者都支持),manualOnly(只允许手动触发)

当然了,如果你不喜欢在布局文件中指定,完全可以使用代码设置,在onCreate里面写:mPullRefreshListView.setMode(Mode.BOTH);//设置你需要的模式

一切伟大的行动和思想,都有一个微不足道的开始

Android PullToRefresh (ListView GridView 下拉刷新) 使用详

相关文章:

你感兴趣的文章:

标签云: