pullToRefreshView下拉刷新上拉加载

     又是好长时间没写博客了,今天用到了下拉刷新下拉加载,总结了下也看了下源码,把下拉刷新搞懂了,上拉加载还在看,今天就不说原理了,而是说一下应该怎么用,其实pullToRefreshView很简单只要把准备工作做好就好了

 一、上拉刷新下拉加载的实现

  1、实现上拉刷新下拉加载目前基本上是采用pulltoRefreshView开源框架来进行实现它的实现方法主要分为两种
  (1).从官网下载jar包添加到项目中 gitHub网址    https://github.com/chrisbanes/Android-PullToRefresh
  (2).把需要的类从源码中抽取出来,把抽取的自己需要的源码放到自己的项目中,我个人比较倾向于这种方法, 因为这种方法可以根据项目的需要进行相应的改动,
     所以我就将(2)种方法的做法说一下

2、实现步骤:

目前已经有人抽好的源码,我们可以直接拿来用,如果想弄清楚里面的实现原理可以看源码,里面源码的注释非常详细
实现步骤:
(1)需要导入的资源
     pullToRefreshView下拉刷新上拉加载_第1张图片          pullToRefreshView下拉刷新上拉加载_第2张图片
如图所示,红色圈住的是我们要加到项目的类里面包括GridView,ListView,ScrollView,WebView我们可以根据需要进行选择
另外需要注意的是我们还需要资源文件下的如图二所示的布局和values下的picture_dimens.xml和pull_refresh_string.xml拷贝过去。做好以上工作,我就可以
根据项目的实际需要对动画,文字等等进行相应的更改,下面以一个实例进行具体的阐述效果图如下:
pullToRefreshView下拉刷新上拉加载_第3张图片  pullToRefreshView下拉刷新上拉加载_第4张图片      pullToRefreshView下拉刷新上拉加载_第5张图片
          
我们可以在 pull_refresh_strings.xml中改变加载时候的文本,可以在 pull_to_load_footer.xml中改变加载到底部的布局
可以在 putt_load_header.xml中改变如上图(1)的动画的图片等等。

3.源码

   

package com.example.pulltorefreshpractice;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import com.example.ui.PullToRefreshBase;
import com.example.ui.PullToRefreshBase.OnRefreshListener;
import com.example.ui.PullToRefreshListView;


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 android.widget.SimpleAdapter;


public class MainActivity extends Activity {
	
	private PullToRefreshListView ptrlv_test;
	private LinkedList<String> mListItem;
	private ArrayAdapter<String> mAdapter;
	
	private ListView mListView;
	
	private boolean isPullDownRefresh=true;//判断是下拉,还是上拉的标记
	private int mCurrentIndex=0;//当前条目的位置
	
	private static final int oneLoadCount=20;//每一次加载条目的个数
	
	private SimpleDateFormat mDateFormat = new SimpleDateFormat("MM-dd HH:mm");//日期格式

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mListItem=new LinkedList<String>();
		mListItem.addAll(Arrays.asList(mStrings).subList(0,oneLoadCount));//从总体中截取一次加载的条目个数进行加载
		
		ptrlv_test=(PullToRefreshListView) findViewById(R.id.ptrlv_test);
		//设置当前上拉加载不可用
		ptrlv_test.setPullLoadEnabled(false);
		//设置下拉刷新可用
		ptrlv_test.setPullRefreshEnabled(true);
		//设置上拉加载可用
		ptrlv_test.setPullLoadEnabled(true);
		//滑到底部是否自动加载数据,这句话一定要加要不然"已经到底啦"显示不出来
		ptrlv_test.setScrollLoadEnabled(true);
		//得到真正的listview,我们在给listview设置adapter时或者设置onItemClick事件必须通过它,而不能用ptrlv_test
		mListView = ptrlv_test.getRefreshableView();
		mCurrentIndex=oneLoadCount;
		mAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,mListItem);
		
		mListView.setAdapter(mAdapter);
		//设置刷新监听器
		ptrlv_test.setOnRefreshListener(new OnRefreshListener<ListView>() {

			@Override
			public void onPullDownToRefresh(
				PullToRefreshBase<ListView> refreshView) {
				isPullDownRefresh=true;
				new getMoreDateTask().execute();
			}

			@Override
			public void onPullUpToRefresh(
				PullToRefreshBase<ListView> refreshView) {
				isPullDownRefresh=false;
				new getMoreDateTask().execute();
			}
			
		});
		setLastUpdateTime();
	}
	
	private void setLastUpdateTime() {
		 String text = formatDateTime(System.currentTimeMillis());
	     ptrlv_test.setLastUpdatedLabel(text);
		
	}
	
	private String formatDateTime(long time) {
        if (0 == time) {
            return "";
        }
        
        return mDateFormat.format(new Date(time));
    }

	//异步加载数据
	private class getMoreDateTask extends AsyncTask<Void, Void, String []>{

		@Override
		protected String[] doInBackground(Void... params) {
			
			try {
				Thread.sleep(2000);//模拟加载数据
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return mStrings;
		}
		
		@Override
		protected  void onPostExecute(String[] result) {
			super.onPostExecute(result);
			boolean hasMoreData=true;
			//判断是上拉,还是下拉
			if(isPullDownRefresh){
				mListItem.addFirst("刷新增加的数据");
			}else{
				//如果是上拉加载从当前条目position+oneLoadCount即在原有的基础上增加onLoadCount个条目
				int start=mCurrentIndex;
				int end=start+oneLoadCount;
				if(end>=mStrings.length){
					end=mStrings.length;
					hasMoreData=false;
				}
					
				for(int i=start;i<end;i++){
				    mListItem.add(mStrings[i]);
				}
				mCurrentIndex=end;
			}
			
			mAdapter.notifyDataSetChanged();
			ptrlv_test.onPullDownRefreshComplete();
			ptrlv_test.onPullUpRefreshComplete();
			//把当前是否是有数据的状态传过去
			ptrlv_test.setHasMoreData(hasMoreData);
		}
	}
	
	
	public static final String[] mStrings = {
		 "1", "2", "3", "4", "5","6", "7", "8", "9", "10", 
	        "11","12", "13", "14", "15", "16","17", "18", "19", "20",
	        "21","22", "23", "24", "25", "26","27", "28", "29", "30", 
	        "31", "32","33", "34", "35", "36", "37", "38", "39","40", 
	        "41", "42", "43", "44","45", "46", "47", "48", "49", "50",
	        "51", "52", "53", "54", "55", "56","57", "58", "59", "60",
	        "61", "62", "63", "64", "65","66", "67", "68", "69","70",
	        "71", "72", "73", "74","75", "76", "77", "78", "79", "80",
	        "81", "82", "83", "84","85", "86", "87", "88", "89", "90",
	        "91", "92", "93", "94","95", "96", "97", "98","99", "100", 
	        "101", "102","103", "104", "105", "106", "107", "108","109", "110", 
	        "111", "112", "113", "114", "115","116", "117", "118", "119", "120",
	        "121", "122", "123", "124", "125","126", "127", "128", "129","130", 
	        "131", "132", "133", "134","135", "136", "137", "138","139", "140", 
	        "141", "142", "143","144", "145", "146", "147", "148","149", "150", 
	        "151", "152", "153","154", "155", "156", "157", "158", "159","160", 
	        "161", "162", "163","164", "165", "166", "167", "168","169", "170", 
	        "171", "172", "173","174", "175", "176", "177", "178","179", "180", 
	        "181", "182", "183","184", "185", "186", "187","188", "189", "190", 
	        "191", "192","193", "194", "195", "196", "197","198", "199", "200", 
	        "201","202", "203", "204", "205", "206", "207","208", "209", "210", 
	        "211", "212","213", "214", "215", "216", "217","218", "219", "220", 
	        "221", "222","223", "224", "225", "226", "227", "228","229", "230", 
	        "231", "232", "233","234", "235", "236", "237","238", "239", "240", 
	        "241", "242","243", "244", "245", "246","247", "248", "249", "250", 
	        "251","252", "253", "254", "255", "256", "257","258", "259", "260",
	        "261", "262", "263", "264", "265","266", "267", "268", "269","270", 
	        "271", "272", "273", "274", "275","276", "277", "278", "279", "280", 
	        "281", "282","283", "284", "285", "286","287", "288", "289", "290",
	        "291","292", "293", "294", "295", "296","297", "298", "299", "300"
	};
}

大家可以把源码下载下来把上面我说的需要导入项目的类和资源文件保存起来,下次用到直接拿来用,就可以了

下载源码请点击


你可能感兴趣的:(源码,android,异步)