1. 程式人生 > >pullToRefreshView下拉重新整理上拉載入,pullrefreshlistview

pullToRefreshView下拉重新整理上拉載入,pullrefreshlistview

 一、上拉重新整理下拉載入的實現

  1、實現上拉重新整理下拉載入目前基本上是採用pulltoRefreshView開源框架來進行實現它的實現方法主要分為兩種   (1).從官網下載jar包新增到專案中 gitHub網址   https://github.com/chrisbanes/Android-PullToRefresh   (2).把需要的類從原始碼中抽取出來,把抽取的自己需要的原始碼放到自己的專案中,我個人比較傾向於這種方法,因為這種方法可以根據專案的需要進行相應的改動,      所以我就將(2)種方法的做法說一下 
2、實現步驟: 

目前已經有人抽好的原始碼,我們可以直接拿來用,如果想弄清楚裡面的實現原理可以看原始碼,裡面原始碼的註釋非常詳細 

實現步驟:(1)需要匯入的資源

 如圖所示,紅色圈住的是我們要加到專案的類裡面包括GridView,ListView,ScrollView,WebView我們可以根據需要進行選擇 另外需要注意的是我們還需要資原始檔下的如圖二所示的佈局和values下的picture_dimens.xml和pull_refresh_string.xml拷貝過去。做好以上工作,我就可以 根據專案的實際需要對動畫,文字等等進行相應的更改,下面以一個例項進行具體的闡述效果圖如下:

           我們可以在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"
	};
}

大家可以把原始碼下載下來把上面我說的需要匯入專案的類和資原始檔儲存起來,下次用到直接拿來用,就可以了