效果如下:
一、安装library
在github上下载包,下载地址:点击打开链接
使用方法:在eclipse中导入解压后的library文件,把此项目设为库(is library),即可使用。
二、Java代码
package com.hxzy.pulltorefresh;
import java.util.ArrayList;
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.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ArrayList<String> data;
private ArrayAdapter adapter;
private int count = 0;
private PullToRefreshListView pListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = new ArrayList<String>();
pListView = (PullToRefreshListView) findViewById(R.id.pListView);
pListView.setMode(Mode.PULL_FROM_START);
pListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
new MyTask().execute();
}
});
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);
pListView.setAdapter(adapter);
TextView text = new TextView(this);
text.setText("请下拉刷新");
pListView.setEmptyView(text);
}
private class MyTask extends AsyncTask {
@Override
protected void onPreExecute() {
pListView.setRefreshing();
}
@Override
protected Object doInBackground(Object... params) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return count++;
}
@Override
protected void onPostExecute(Object result) {
data.add(0, result + "");
pListView.setLastUpdatedLabel("last refresh time is:" + System.currentTimeMillis());
adapter.notifyDataSetChanged();
pListView.onRefreshComplete();
}
}
}
三、layout布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hxzy.pulltorefresh.MainActivity" >
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pListView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
四、加载数字和网络图片的Java代码
package com.hxzy.pulltorefresh_advanced;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.HttpConnection;
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.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private int count = 0;
private ArrayList<HashMap<String, Object>> data;
private PullToRefreshListView pListView;
private ArrayAdapter adapter;
private final String KEY_TEXT = "key_text", KEY_IMAGE = "key_image";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = new ArrayList<HashMap<String, Object>>();
pListView = (PullToRefreshListView) findViewById(R.id.pListView);
pListView.setMode(Mode.PULL_FROM_START);
pListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
new Mytask().execute();
}
});
adapter = new MyAdapter(this, -1);
pListView.setAdapter(adapter);
TextView textView = new TextView(this);
textView.setText("请下拉刷新");
pListView.setEmptyView(textView);
}
class Mytask extends AsyncTask {
@Override
protected void onPreExecute() {
pListView.setRefreshing();
}
@Override
protected Object doInBackground(Object... params) {
HashMap<String, Object> map = new HashMap<String, Object>();
byte[] buf = null;
try {
buf = loadRawDataFromURL("http://avatar.youkuaiyun.com/9/7/A/1_zhangphil.jpg");
Bitmap bitmap = BitmapFactory.decodeByteArray(buf, 0, buf.length);
map.put(KEY_IMAGE, bitmap);
map.put(KEY_TEXT, count++);
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
@Override
protected void onPostExecute(Object result) {
data.add(0, (HashMap<String, Object>) result);
pListView.setLastUpdatedLabel("last refresh time is:" + System.currentTimeMillis());
adapter.notifyDataSetChanged();
pListView.onRefreshComplete();
}
}
class MyAdapter extends ArrayAdapter {
private LayoutInflater flater;
public MyAdapter(Context context, int resource) {
super(context, resource);
flater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public HashMap<String, Object> getItem(int position)
{
return (HashMap<String, Object>) (data.get(position));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = flater.inflate(R.layout.item, null);
HashMap<String, Object> map = getItem(position);
ImageView image = (ImageView) convertView.findViewById(R.id.image);
image.setImageBitmap((Bitmap) map.get(KEY_IMAGE));
TextView text = (TextView) convertView.findViewById(R.id.text);
text.setText(map.get(KEY_TEXT) + "");
return convertView;
}
}
public static byte[] loadRawDataFromURL(String u) throws Exception {
URL url = new URL(u);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
final int EOF = -1;
final int BUF_SIZE = 2 * 1024;
int c = 0;
byte[] buf = new byte[BUF_SIZE];
while (true) {
c = bis.read(buf);
if (c == EOF)
break;
baos.write(buf, 0, c);
}
conn.disconnect();
is.close();
byte[] buffer = baos.toByteArray();
baos.flush();
return buffer;
}
}