andriod PullToRefresh的使用 构建下拉刷新的listView

下载源码后(https://github.com/chrisbanes/Android-PullToRefresh),里面有个Library工程,添加工程到Eclipse中;

1.导入Library 







2.布局xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<!--     The PullToRefreshListView replaces a standard ListView widget. -->

    <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"
        ptr:ptrMode="both" //上下都能刷新
         />
</LinearLayout>


3.java代码

package com.example.pulltoflush;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import android.os.AsyncTask;
import android.os.Bundle;
 
import android.app.Activity;
 
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    private PullToRefreshListView mPullRefreshListView;

    
    private ArrayAdapter<String> mArrayAdpter;
    private List<String> mArraylist=new ArrayList<String>();
 
    protected void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
        
        mArrayAdpter=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,mArraylist);
        mPullRefreshListView.setAdapter(mArrayAdpter);
  
    	mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				
				new GetDataTask().execute();
			}
 
		});

    }

 
	private class GetDataTask extends AsyncTask<Void, Void, String> {


		protected String doInBackground(Void... arg0)
		{
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
			} 
			return shuangsheqiu();
		}
		
 		
		protected void onPostExecute(String result) {
			mArraylist.add(result);
			mArrayAdpter.notifyDataSetChanged();
			mPullRefreshListView.onRefreshComplete();
			super.onPostExecute(result);
	 
		}
		
	}
 
	private String shuangsheqiu()
	{
		// TODO Auto-generated method stub
		
		Set<String> treeSet=new TreeSet<String>();
	
		do
		{
			treeSet.add(getredball());
 
		} while (treeSet.size()!=6);
		
		String[] arrayString=new String[6];
		arrayString=treeSet.toArray(arrayString);
		
		StringBuilder resb=new StringBuilder();
		resb.append("[第"+(mArraylist.size()+1)+"组] ");
		
		for(int i=0;i<6;i++){
			resb.append(arrayString[i]);
			resb.append("-");
		}
		
		resb.append("+蓝:");
		resb.append(getbulball());
 
		 return resb.toString();
	}
	
	
	private String getredball(){
		
		 int i= (int)(Math.random()*33+1);
		 if(i<10){
			 return "0"+String.valueOf(i);
		 }else{
			 return String.valueOf(i);
		 }
	}
	
	
	private String getbulball(){
		
		 int i= (int)(Math.random()*16+1);
		 if(i<10){
			 return "0"+String.valueOf(i);
		 }else{
			 return String.valueOf(i);
		 }
	}
    
}


(1)派生自AsyncTask

由于派生自AsyncTask,所以下面的那个函数doInBackground和onPostExecute就不难理解了,这两个函数是AsyncTask必须是重写的两个函数
(2)doInBackground函数

doInBackground执行要于后台执行的语句,返回的值可以是任意类型,但要提前在extends AsyncTask<Void, Void, String> 中定义,这个返回值会做为onPostExecute的参数result传到onPostExecute函数中;如果对于网络访问程序,doInBackground就执行访问网络的代码,然后讲返回值存在result中传给onPostExecute函数,以刷新列表;

 (3)onPostExecute函数

 onPostExecute()是对返回的值进行操作,并添加到ListView的列表中,有两种添加方式添加到头部----mArraylist.addFirst(result);和添加在尾部----

mArraylist<span style="font-family: Arial; background-color: rgb(255, 255, 255);">.addLast(result);</span>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值