pulltorefresh的使用

在android中,有时候需要用到listview的下拉刷新,自己写一个可能很麻烦,而且很耗时,所以可以直接利用开源的pulltorefresh,组件进行刷新

下载链接:https://github.com/chrisbanes/Android-PullToRefresh

解压缩以后将,library文件夹,引入到eclipse中,在新建的项目中:右键---->properties----->android------->add添加之前引入的library

自定义的布局如下;list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    
    <ImageView 
        android:id="@+id/imageid"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        />
	<TextView 
	    android:id="@+id/textid"
	    android:layout_weight="4"
	    android:layout_width="0dp"
	    android:layout_height="wrap_content"
	    android:gravity="center"
	    />
</LinearLayout>

在activity_main.xml布局中引入library中的刷新组件,activity_main.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
        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" />

</LinearLayout>

MainActivity.java如下:

package com.example.pulltorefresh;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
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.database.DataSetObserver;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {
	private List<Map<String,String>>list = new ArrayList<Map<String,String>>();
	private PullToRefreshListView pullView;
	private String[]texts = new String[]{"aaaa","bbbb","cccc","dddd","eeee","ffff","gggg"};
	private int[] images = new int[]{R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher
			,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
	SimpleAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initList();
		pullView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
        // set mode to BOTH
		pullView.setMode(Mode.PULL_UP_TO_REFRESH);
		pullView.getLoadingLayoutProxy(false, true).setPullLabel(getString(R.string.pull_to_load));
		pullView.getLoadingLayoutProxy(false, true).setRefreshingLabel(getString(R.string.loading));
		pullView.getLoadingLayoutProxy(false, true).setReleaseLabel(getString(R.string.release_to_load));
		
        // 滚动到底自动加载可用  
		adapter = new SimpleAdapter(this, list,R.layout.list_item,new String[]{"imageid","textid"},new int[]{R.id.imageid,R.id.textid});
		ListView actualListView = pullView.getRefreshableView();  
        actualListView.setAdapter(adapter);  

		pullView.setOnRefreshListener(new OnRefreshListener<ListView>() {

			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				// TODO Auto-generated method stub
				String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
						DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

				// Update the LastUpdatedLabel
				refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
				new GetDataTask().execute(new Void[]{});
			}
		});

	}

	//初始化list的数据
	private void initList() {
		// TODO Auto-generated method stub
		for (int i = 0; i < texts.length; i++) {
			Map<String,String>map = new HashMap<String, String>();
			map.put("imageid",images[i]+"");
			map.put("textid",texts[i]);
			list.add(map);
		}
	}

	public class GetDataTask extends AsyncTask<Void,Void,Void> {

		@Override
		protected Void doInBackground(Void... arg0) {
			// TODO Auto-generated method stub
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			for (int i = 0; i < 2; i++) {//每次加载两个数据
				Map<String,String>map = new HashMap<String, String>();
				map.put("imageid",R.drawable.ic_launcher+"");
				map.put("textid","fresh data"+i);
				list.add(map);
			}
		
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			adapter.notifyDataSetChanged();
			pullView.onRefreshComplete();
			super.onPostExecute(result);
		}
	}

}

你可能感兴趣的:(Android开发,ListView,开源)