频道栏目
首页 > 程序开发 > 移动开发 > Android > 正文
开源插件 PullToRefresh: PullToRefreshListView / PullToRefreshGridView 实例详解
2016-04-13 10:11:31         来源:可乐淘的专栏  
收藏   我要投稿

AndroidManifest.xml



    

    
        
            
                

                
            
        
        
        
        
        
    



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



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 mListItems;
	
	private PullToRefreshListView mPullRefreshListView;
	private ArrayAdapter 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();
		for (int i = 0; i < mItemCount; i++) {
			mListItems.add("" + i);
		}
	}

	/**
	 * 初始化界面
	 */
	private void initView() {
		// 得到控件
		mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
		// 设置适配器
		mAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, mListItems);
		mPullRefreshListView.setAdapter(mAdapter);
	}
	
	/**
	 * 设置监听事件
	 * 		下拉刷新的回调:setOnRefreshListener(new OnRefreshListener(){}
	 */
	private void setListener() {
		mPullRefreshListView.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh(PullToRefreshBase 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 {
		@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

 



    
    



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 mListItems;
	
	private PullToRefreshListView mPullRefreshListView;
	private ArrayAdapter 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();
		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(this, android.R.layout.simple_list_item_1, mListItems);
		mPullRefreshListView.setAdapter(mAdapter);
		
	}

	/**
	 * 设置监听事件 
	 * 		下拉、上拉刷新的回调setOnRefreshListener(new OnRefreshListener2(){});
	 * 		interface OnRefreshListener2 这个接口包含两个方法,一个上拉回调,一个下拉回调
	 * 注:如果上拉和下拉需求是执行一样的代码,那么可以使用回调setOnRefreshListener(new OnRefreshListener(){},上拉和下拉都会执行同一个方法,前提是要设置mPullRefreshListView.setMode(Mode.BOTH)或xml属性ptr:ptrMode="both" 。
	 */
	private void setListener() {
		mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2() {
			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				Log.e(TAG, "onPullDownToRefresh");
				// 这里写下拉刷新的任务
				new GetDataTask().execute();
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				Log.e(TAG, "onPullUpToRefresh");
				// 这里写上拉加载更多的任务
				new GetDataTask().execute();
			}
		});
	}

	/**
	 * 模拟加载任务 在回调中模拟了一个异步任务,加载了一个Item
	 */
	private class GetDataTask extends AsyncTask {
		@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 mListItems;
	private PullToRefreshGridView mPullRefreshListView;
	private ArrayAdapter 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();
		for (int i = 0; i < mItemCount; i++) {
			mListItems.add("" + i);
		}
	}

	/**
	 * 初始化界面
	 */
	private void initView() {
		// 得到控件
		mPullRefreshListView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
		// 设置适配器
		mAdapter = new ArrayAdapter(this, R.layout.item_grid, R.id.id_grid_item_text, mListItems);
		mPullRefreshListView.setAdapter(mAdapter);

	}

	/**
	 * 设置监听事件 下拉、上拉刷新的回调setOnRefreshListener(new OnRefreshListener2(){}); 
	 * 		interface OnRefreshListener2 这个接口包含两个方法,一个上拉回调,一个下拉回调
	 * 		注:如果上拉和下拉需求是执行一样的代码,那么可以使用回调setOnRefreshListener(new OnRefreshListener(){},上拉和下拉都会执行同一个方法,
	 * 		前提是要设置mPullRefreshListView.setMode(Mode.BOTH)或xml属性ptr:ptrMode="both" 。
	 */
	private void setListener() {

		mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase 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 refreshView) {
				Log.e(TAG, "onPullUpToRefresh"); 
				new GetDataTask().execute();  
			}
		});

	}

	/**
	 * 模拟加载任务 在回调中模拟了一个异步任务,加载了一个Item
	 */
	private class GetDataTask extends AsyncTask {
		@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


    





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 mListItems;
	
	private PullToRefreshListView mPullRefreshListView;
	private ArrayAdapter 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();
		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(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(){});
	 * 		interface OnRefreshListener2 这个接口包含两个方法,一个上拉回调,一个下拉回调
	 * 注:如果上拉和下拉需求是执行一样的代码,那么可以使用回调setOnRefreshListener(new OnRefreshListener(){},上拉和下拉都会执行同一个方法,前提是要设置mPullRefreshListView.setMode(Mode.BOTH)或xml属性ptr:ptrMode="both" 。
	 */
	private void setListener() {
		mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2() {
			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				Log.e(TAG, "onPullDownToRefresh");
				// 这里写下拉刷新的任务
				new GetDataTask().execute();
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				Log.e(TAG, "onPullUpToRefresh");
				// 这里写上拉加载更多的任务
				new GetDataTask().execute();
			}
		});
	}

	/**
	 * 模拟加载任务 在回调中模拟了一个异步任务,加载了一个Item
	 */
	private class GetDataTask extends AsyncTask {
		@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();
		}
	}

}						
点击复制链接 与好友分享!回本站首页
相关TAG标签 插件 实例
上一篇:Android 上千实例源码分析以及开源分析
下一篇:Android的动画类型
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站