Android 第三方开源PullToRefresh加入Item(系列2)

效果如图:


java代码:

package com.example.pulltorefresh;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
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 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.view.LayoutInflater;
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 PullToRefreshListView mPullListView;
	private ArrayList<HashMap<String, Object>> list;
	private ArrayAdapter adapter;
	private final String IMAGE_1 = "image_1";
	private final String IMAGE_2 = "iamge_2";
	private final String TEXT = "text";
	private int count = 0;

	private String[] uris = { "http://v1.qzone.cc/avatar/201408/20/17/23/53f468ff9c337550.jpg%21200x200.jpg",
			"http://h.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=0196e54bfefaaf5184b689bbb964b8d8/f7246b600c33874424756211520fd9f9d72aa02f.jpg",
			"http://yun.hainei.org/forum/201511/08/113241tp76krzpxcc6pxxg.jpg",
			"http://yun.hainei.org/forum/201511/08/113241thqrtvj7qhjbdarj.jpg", };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		list = new ArrayList<HashMap<String, Object>>();

		mPullListView = (PullToRefreshListView) findViewById(R.id.pulllistview);

		mPullListView.setMode(Mode.PULL_FROM_START);

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

			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				new MyAsyncTask().execute();
			}
		});

		adapter = new MyAdapter(this, R.layout.pulllist_item);
		mPullListView.setAdapter(adapter);
	}

	private class MyAsyncTask extends AsyncTask {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();

			mPullListView.setRefreshing();
		}

		@Override
		protected Object doInBackground(Object... params) {

			byte[] buffer = getByteFromUri(uris[count]);

			Bitmap bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put(IMAGE_1, bitmap);
			map.put(IMAGE_2, bitmap);
			map.put(TEXT, "测试数据" + count++);

			if (count == 4) {
				count = 0;
			}

			return map;
		}

		@Override
		protected void onPostExecute(Object result) {
			HashMap<String, Object> map = (HashMap<String, Object>) result;

			list.add(0, map);
			adapter.notifyDataSetChanged();

			mPullListView.onRefreshComplete();

		}
	}

	private byte[] getByteFromUri(String uri) {

		URL url;
		try {
			url = new URL(uri);

			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			conn.setConnectTimeout(3000);
			conn.setReadTimeout(3000);

			InputStream in = conn.getInputStream();
			BufferedInputStream bis = new BufferedInputStream(in);

			ByteArrayOutputStream bos = new ByteArrayOutputStream();

			final int BUFFER_LENGTH = 2 * 1024;
			final int EOF = -1;

			byte[] buffer = new byte[BUFFER_LENGTH];
			int c;

			while (true) {

				c = bis.read(buffer);
				if (c == EOF)
					break;

				bos.write(buffer, 0, c);

			}
			bis.close();
			bos.flush();
			byte[] data = bos.toByteArray();

			return data;
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {

		}
		return null;
	}

	private class MyAdapter extends ArrayAdapter {

		private Context mContext;
		private LayoutInflater inflater;
		private int resource;

		public MyAdapter(Context context, int textViewResourceId) {
			super(context, textViewResourceId);

			this.mContext = context;
			this.resource = textViewResourceId;

			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return list.size();
		}

@Override
public View getView(int position, View convertView, ViewGroup parent) {


HashMap<String, Object> map = getItem(position);


if (convertView == null)
convertView = inflater.inflate(resource, null);


ImageView image1 = (ImageView) convertView.findViewById(R.id.image1);
ImageView image2 = (ImageView) convertView.findViewById(R.id.image2);
TextView text = (TextView) convertView.findViewById(R.id.text);


image1.setImageBitmap((Bitmap) map.get(IMAGE_1));
image2.setImageBitmap((Bitmap) map.get(IMAGE_2));
text.setTe
}
	}

	xt(map.get(TEXT) + "");


return convertView;
}

@Override
public HashMap<String, Object> getItem(int position) {
return list.get(position);
}

需要的布局文件xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:id="@+id/image1"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:layout_alignParentLeft="true"
        android:src="@drawable/ic_launcher" />


    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/image1"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/image2"
        android:layout_toRightOf="@+id/image1"
        android:gravity="center"
        android:textSize="20sp" />


    <ImageView
        android:id="@+id/image2"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_launcher" />


</RelativeLayout>


你可能感兴趣的:(Android 第三方开源PullToRefresh加入Item(系列2))