下拉刷新图片


package com.example.pulltorefresh;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
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	ArrayList<HashMap<String, Object>> data;
	private	int count=0;
	private	ArrayAdapter adapter;
	private	PullToRefreshListView mListView;
	
	private final String KEY_IMAGE = "key_image", KEY_TEXT = "key_text";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		
		data=new ArrayList<HashMap<String, Object>>();
		
		mListView=(PullToRefreshListView) findViewById(R.id.list_view);
		mListView.setMode(Mode.PULL_FROM_START);
		mListView.setOnRefreshListener(new OnRefreshListener<ListView>(){

			@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				//完成你的业务逻辑
				new MyTask().execute();
			}
		});
		
		adapter=new MyAdapter(this,-1);
		mListView.setAdapter(adapter);
		
		TextView text=new TextView(this);
		text.setText("请下拉刷新");
		mListView.setEmptyView(text);
	}
	
	private class MyAdapter extends ArrayAdapter{
		private LayoutInflater inflater;
		
		public MyAdapter(Context context, int resource) {
			super(context, resource);
			inflater = LayoutInflater.from(context);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			if(convertView == null)
				convertView = inflater.inflate(R.layout.item,null);

			HashMap<String,Object> map = getItem(position);
			
			ImageView image = (ImageView) convertView.findViewById(R.id.imageView);		
			image.setImageBitmap((Bitmap)map.get(KEY_IMAGE));
			
			TextView text = (TextView) convertView.findViewById(R.id.textView);
			text.setText(map.get(KEY_TEXT) + "");
			
			return convertView;
		}

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

		@Override
		public int getCount() {
			
			return data.size();
		}
	}
	
	private	class	MyTask extends	AsyncTask{

		@Override
		protected void onPreExecute() {
			mListView.setRefreshing();
		}
		
		@Override
		protected Object doInBackground(Object... params) {
			
			HashMap<String, Object> map = new HashMap<String, Object>();
			
			try {
				byte [] buf = loadRawDataFromURL("http://pic16.nipic.com/20110908/6910138_102825181129_2.jpg");
			
				Bitmap bmp = BitmapFactory.decodeByteArray(buf, 0, buf.length);
				
				map.put(KEY_IMAGE, bmp);
				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);
			
			mListView.setLastUpdatedLabel("最后更新时间:"+new Date());
			
			adapter.notifyDataSetChanged();
			mListView.onRefreshComplete();
		}
	}	
	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 BUFFER_SIZE = 2048;
		final int EOF = -1;

		int c;
		byte[] buf = new byte[BUFFER_SIZE];

		while (true) {
			c = bis.read(buf);
			if (c == EOF)
				break;

			baos.write(buf, 0, c);
		}

		conn.disconnect();
		is.close();

		byte[] data = baos.toByteArray();
		baos.flush();

		return data;
	}
}

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.pulltorefresh.MainActivity" >

     <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
     
</RelativeLayout>

<?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="vertical" >
    
<TextView
        android:padding="10dip"
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="id" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    
</LinearLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值