参考:http://blog.csdn.net/lmj623565791/article/details/38238749
依赖lib下载地址:https://github.com/chrisbanes/Android-PullToRefresh
本博用实例说话!
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cctvjiatao.pulltorefreshlistviewdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".OnlyPullDownAct"></activity>
<activity android:name=".BothPullDownUpAct"></activity>
<activity android:name=".PullGridpAct"></activity>
<activity android:name=".PullWithCustomTextAct"></activity>
</application>
</manifest>
MainActivity.java
package com.cctvjiatao.pulltorefreshlistviewdemo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* @作者: jiatao
* @修改时间:2016-4-12 下午6:03:08
* @文件名:MainActivity.java
* @版权声明:www.cctvjiatao.com
* @功能: 开源插件 PullToRefresh, PullToRefreshListView / PullToRefreshGridView 实例详解
*/
public class MainActivity extends Activity {
private Context context;
private Button btn1, btn2, btn3, btn4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.act_main);
initView();
}
private void initView() {
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(context, OnlyPullDownAct.class));
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(context, BothPullDownUpAct.class));
}
});
btn3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(context, PullGridpAct.class));
}
});
btn4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(context, PullWithCustomTextAct.class));
}
});
}
}
act_main.xml
<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">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="只能下拉刷新"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下拉上拉都能刷新"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="带下拉和上拉的GridView"/>
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义指示器文本内容"/>
</LinearLayout>
OnlyPullDownAct.java
package com.cctvjiatao.pulltorefreshlistviewdemo;
import java.util.LinkedList;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
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;
/**
* @作者: jiatao
* @修改时间:2016-4-12 下午11:39:50
* @包名:com.cctvjiatao.pulltorefreshlistviewdemo
* @文件名:OnlyPullDownAct.java
* @版权声明:www.cctvjiatao.com
* @功能: 只能下拉的 PullToRefreshListView
*/
public class OnlyPullDownAct extends Activity {
private final String TAG = getClass().getSimpleName();
private LinkedList<String> mListItems;
private PullToRefreshListView mPullRefreshListView;
private ArrayAdapter<String> mAdapter;
private int mItemCount = 9;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_pulllist);
initDatas();
initView();
setListener();
}
/**
* 初始化数据
*/
private void initDatas() {
mListItems = new LinkedList<String>();
for (int i = 0; i < mItemCount; i++) {
mListItems.add("" + i);
}
}
/**
* 初始化界面
*/
private void initView() {
// 得到控件
mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
// 设置适配器
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);
mPullRefreshListView.setAdapter(mAdapter);
}
/**
* 设置监听事件
* 下拉刷新的回调:setOnRefreshListener(new OnRefreshListener<ListView>(){}
*/
private void setListener() {
mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
Log.e(TAG, "onRefresh");
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();// 模拟加载任务
}
});
}
/**
* 模拟加载任务
* 在回调中模拟了一个异步任务,加载了一个Item
*/
private class GetDataTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
return "" + (mItemCount++);
}
@Override
protected void onPostExecute(String result) {
mListItems.add(result);
mAdapter.notifyDataSetChanged();
mPullRefreshListView.onRefreshComplete();
}
}
}
act_pulllist.xml
<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" >
<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="#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>
<!--
常用的一些属性:
ptr:ptrMode="both" 设置上拉和下拉都支持。
可选值为:disabled(禁用下拉刷新), pullFromStart(仅支持下拉刷新), pullFromEnd(仅支持上拉刷新), both(二者都支持), manualOnly(只允许手动触发)
也可以使用代码设置,在onCreate里面写:mPullRefreshListView.setMode(Mode.BOTH);//设置你需要的模式
设置图标 ptr:ptrDrawable="@drawable/ic_launcher"
设置动画 ptr:ptrAnimationStyle="flip" 倒置、ptr:ptrAnimationStyle="rotate"旋转
ptrRefreshableViewBackground 设置整个mPullRefreshListView的背景色
ptrHeaderBackground 设置下拉Header或者上拉Footer的背景色
ptrHeaderTextColor 用于设置Header与Footer中文本的颜色
ptrHeaderSubTextColor 用于设置Header与Footer中上次刷新时间的颜色
ptrShowIndicator 如果为true会在mPullRefreshListView中出现icon,右上角和右下角,挺有意思的。
ptrHeaderTextAppearance , ptrSubHeaderTextAppearance分别设置拉Header或者上拉Footer中字体的类型颜色等等。
ptrRotateDrawableWhilePulling 当动画设置为rotate时,下拉是是否旋转。
ptrScrollingWhileRefreshingEnabled 刷新的时候,是否允许ListView或GridView滚动。觉得为true比较好。
ptrListViewExtrasEnabled 决定了Header,Footer以何种方式加入mPullRefreshListView,true为headView方式加入,就是滚动时刷新头部会一起滚动。
注:上述属性很多都可以代码控制,如果有需要可以直接mPullRefreshListView.set属性名 查看
-->
BothPullDownUpAct.java
package com.cctvjiatao.pulltorefreshlistviewdemo;
import java.util.LinkedList;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
/**
* @作者: jiatao
* @修改时间:2016-4-12 下午11:39:50
* @包名:com.cctvjiatao.pulltorefreshlistviewdemo
* @文件名:BothPullDownUpAct.java
* @版权声明:www.cctvjiatao.com
* @功能: 能上、下拉的 PullToRefreshListView
*/
public class BothPullDownUpAct extends Activity {
private final String TAG = getClass().getSimpleName();
private LinkedList<String> mListItems;
private PullToRefreshListView mPullRefreshListView;
private ArrayAdapter<String> mAdapter;
private int mItemCount = 9;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_pulllist);
initDatas();
initView();
setListener();
}
/**
* 初始化数据
*/
private void initDatas() {
mListItems = new LinkedList<String>();
for (int i = 0; i < mItemCount; i++) {
mListItems.add("" + i);
}
}
/**
* 初始化界面
*/
private void initView() {
// 得到控件
mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
mPullRefreshListView.setMode(Mode.BOTH);
// 设置适配器
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);
mPullRefreshListView.setAdapter(mAdapter);
}
/**
* 设置监听事件
* 下拉、上拉刷新的回调setOnRefreshListener(new OnRefreshListener2<ListView>(){});
* interface OnRefreshListener2<V extends View> 这个接口包含两个方法,一个上拉回调,一个下拉回调
* 注:如果上拉和下拉需求是执行一样的代码,那么可以使用回调setOnRefreshListener(new OnRefreshListener<ListView>(){},上拉和下拉都会执行同一个方法,前提是要设置mPullRefreshListView.setMode(Mode.BOTH)或xml属性ptr:ptrMode="both" 。
*/
private void setListener() {
mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
Log.e(TAG, "onPullDownToRefresh");
// 这里写下拉刷新的任务
new GetDataTask().execute();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
Log.e(TAG, "onPullUpToRefresh");
// 这里写上拉加载更多的任务
new GetDataTask().execute();
}
});
}
/**
* 模拟加载任务 在回调中模拟了一个异步任务,加载了一个Item
*/
private class GetDataTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
return "" + (mItemCount++);
}
@Override
protected void onPostExecute(String result) {
mListItems.add(result);
mAdapter.notifyDataSetChanged();
mPullRefreshListView.onRefreshComplete();
}
}
}
PullGridpAct.java
package com.cctvjiatao.pulltorefreshlistviewdemo;
import java.util.LinkedList;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.GridView;
/**
* @作者: jiatao
* @修改时间:2016-4-12 下午11:39:50
* @包名:com.cctvjiatao.pulltorefreshlistviewdemo
* @文件名:PullGridpAct.java
* @版权声明:www.cctvjiatao.com
* @功能: 能上、下拉的 PullToRefreshGridView
*/
public class PullGridpAct extends Activity {
private final String TAG = getClass().getSimpleName();
private LinkedList<String> mListItems;
private PullToRefreshGridView mPullRefreshListView;
private ArrayAdapter<String> mAdapter;
private int mItemCount = 9;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_pullgrid);
initDatas();
initView();
setListener();
}
/**
* 初始化数据
*/
private void initDatas() {
mListItems = new LinkedList<String>();
for (int i = 0; i < mItemCount; i++) {
mListItems.add("" + i);
}
}
/**
* 初始化界面
*/
private void initView() {
// 得到控件
mPullRefreshListView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
// 设置适配器
mAdapter = new ArrayAdapter<String>(this, R.layout.item_grid, R.id.id_grid_item_text, mListItems);
mPullRefreshListView.setAdapter(mAdapter);
}
/**
* 设置监听事件 下拉、上拉刷新的回调setOnRefreshListener(new OnRefreshListener2<GridView>(){});
* interface OnRefreshListener2<V extends View> 这个接口包含两个方法,一个上拉回调,一个下拉回调
* 注:如果上拉和下拉需求是执行一样的代码,那么可以使用回调setOnRefreshListener(new OnRefreshListener<GridView >(){},上拉和下拉都会执行同一个方法,
* 前提是要设置mPullRefreshListView.setMode(Mode.BOTH)或xml属性ptr:ptrMode="both" 。
*/
private void setListener() {
mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2<GridView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
Log.e(TAG, "onPullDownToRefresh");
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();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
Log.e(TAG, "onPullUpToRefresh");
new GetDataTask().execute();
}
});
}
/**
* 模拟加载任务 在回调中模拟了一个异步任务,加载了一个Item
*/
private class GetDataTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
return "" + (mItemCount++);
}
@Override
protected void onPostExecute(String result) {
mListItems.add(result);
mAdapter.notifyDataSetChanged();
mPullRefreshListView.onRefreshComplete();
}
}
}
act_pullgrid.xml
<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" >
<com.handmark.pulltorefresh.library.PullToRefreshGridView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/pull_refresh_grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="100dp"
android:gravity="center_horizontal"
android:horizontalSpacing="1dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp"
ptr:ptrDrawable="@drawable/ic_launcher"
ptr:ptrAnimationStyle="flip"
ptr:ptrMode="both" />
</RelativeLayout>
<!--
设置图标 ptr:ptrDrawable="@drawable/ic_launcher"
设置动画 ptr:ptrAnimationStyle="flip" 倒置、ptr:ptrAnimationStyle="rotate"旋转
-->
PullWithCustomTextAct.java
package com.cctvjiatao.pulltorefreshlistviewdemo;
import java.util.LinkedList;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
/**
* @作者: jiatao
* @修改时间:2016-4-12 下午11:39:50
* @包名:com.cctvjiatao.pulltorefreshlistviewdemo
* @文件名:PullWithCustomTextAct.java
* @版权声明:www.cctvjiatao.com
* @功能: 设定指示器文本内容
*/
public class PullWithCustomTextAct extends Activity {
private final String TAG = getClass().getSimpleName();
private LinkedList<String> mListItems;
private PullToRefreshListView mPullRefreshListView;
private ArrayAdapter<String> mAdapter;
private int mItemCount = 9;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_pulllist);
initDatas();
initView();
setListener();
}
/**
* 初始化数据
*/
private void initDatas() {
mListItems = new LinkedList<String>();
for (int i = 0; i < mItemCount; i++) {
mListItems.add("" + i);
}
}
/**
* 初始化界面
*/
private void initView() {
// 得到控件
mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
mPullRefreshListView.setMode(Mode.BOTH);
//设定指示器文本内容:上拉和下拉的指示器同时改变
initIndicator();
//设定指示器文本内容:上拉和下拉的指示器内容单独改变
// initIndicator2();
// 设置适配器
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);
mPullRefreshListView.setAdapter(mAdapter);
}
/**
* 设定指示器文本内容
* 默认是上拉和下拉的指示器内容同时改变
*/
private void initIndicator() {
ILoadingLayout iLoadingLayout = mPullRefreshListView.getLoadingLayoutProxy();
iLoadingLayout.setPullLabel("你可劲拉,拉...");// 刚下拉时,显示的提示
iLoadingLayout.setRefreshingLabel("好嘞,正在刷新...");// 刷新时
iLoadingLayout.setReleaseLabel("你敢放,我就敢刷新...");// 下来达到一定距离时,显示的提示
String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
iLoadingLayout.setLastUpdatedLabel(label);// 显示最后更新的时间
}
/**
* 设定指示器文本内容
* 上拉和下拉的指示器内容单独改变
* mPullRefreshListView.getLoadingLayoutProxy(true, false) 下拉的指示器
* mPullRefreshListView.getLoadingLayoutProxy(false, true) 上拉的指示器
*/
private void initIndicator2() {
ILoadingLayout startLabels = mPullRefreshListView.getLoadingLayoutProxy(true, false);
startLabels.setPullLabel("你可劲拉,拉...");// 刚下拉时,显示的提示
startLabels.setRefreshingLabel("好嘞,正在刷新...");// 刷新时
startLabels.setReleaseLabel("你敢放,我就敢刷新...");// 下来达到一定距离时,显示的提示
ILoadingLayout endLabels = mPullRefreshListView.getLoadingLayoutProxy(false, true);
endLabels.setPullLabel("你可劲拉,拉2...");// 刚下拉时,显示的提示
endLabels.setRefreshingLabel("好嘞,正在刷新2...");// 刷新时
endLabels.setReleaseLabel("你敢放,我就敢刷新2...");// 下来达到一定距离时,显示的提示
}
/**
* 设置监听事件
* 下拉、上拉刷新的回调setOnRefreshListener(new OnRefreshListener2<ListView>(){});
* interface OnRefreshListener2<V extends View> 这个接口包含两个方法,一个上拉回调,一个下拉回调
* 注:如果上拉和下拉需求是执行一样的代码,那么可以使用回调setOnRefreshListener(new OnRefreshListener<ListView>(){},上拉和下拉都会执行同一个方法,前提是要设置mPullRefreshListView.setMode(Mode.BOTH)或xml属性ptr:ptrMode="both" 。
*/
private void setListener() {
mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
Log.e(TAG, "onPullDownToRefresh");
// 这里写下拉刷新的任务
new GetDataTask().execute();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
Log.e(TAG, "onPullUpToRefresh");
// 这里写上拉加载更多的任务
new GetDataTask().execute();
}
});
}
/**
* 模拟加载任务 在回调中模拟了一个异步任务,加载了一个Item
*/
private class GetDataTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
return "" + (mItemCount++);
}
@Override
protected void onPostExecute(String result) {
mListItems.add(result);
mAdapter.notifyDataSetChanged();
mPullRefreshListView.onRefreshComplete();
}
}
}