利用第三方开源框架 PullToRefreshListView 实现下拉刷新(从网上下载图片)

来源:互联网 发布:java分销微商城源码 编辑:程序博客网 时间:2024/05/22 13:39

基本原理同 :利用第三方开源框架 PullToRefreshListView 实现下拉刷新; 效果如下:



layout_main.xml :

<LinearLayout 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:orientation="horizontal"    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.android.flush.MainActivity" >    <com.handmark.pulltorefresh.library.PullToRefreshListView        android:id="@+id/listView"        android:layout_width="match_parent"        android:layout_height="match_parent"/>    </LinearLayout>


为 adapter 提供的列表项:

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/imageView"        android:scaleType="fitCenter"        android:layout_width="60dp"        android:layout_height="60dp"/>    <TextView         android:id="@+id/textView"        android:gravity="center"        android:layout_width="match_parent"        android:layout_height="60dp"/>    </LinearLayout>



package com.android.flush;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.HashMap;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.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 final String KEY_IMAGE = "image", KEY_TEXT = "text";private int count = 0;private PullToRefreshListView listView;private ArrayAdapter<String> adapter;private ArrayList<Map<String, Object>> mDatas; private Map<String, Object> map;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView = (PullToRefreshListView)findViewById(R.id.listView);mDatas = new ArrayList<Map<String, Object>>();adapter = new MyAdapter(this, -1);// 设置刷新模式listView.setMode(Mode.PULL_FROM_START);listView.setOnRefreshListener(new OnRefreshListener<ListView>() {@Overridepublic void onRefresh(PullToRefreshBase<ListView> refreshView) {new MyTask().execute();}});listView.setAdapter(adapter);// 设置提示信息TextView textView = new TextView(this);textView.setText("下拉刷新");listView.setEmptyView(textView);}private class MyAdapter extends ArrayAdapter{private LayoutInflater inflater;public MyAdapter(Context context, int resource) {super(context, resource);inflater = getLayoutInflater();}@Overridepublic int getCount() {return mDatas.size();}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if(convertView == null)convertView = inflater.inflate(R.layout.item, null);HashMap<String, Object> map = (HashMap<String, Object>) (mDatas.get(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;}}private class MyTask extends AsyncTask{@Overrideprotected void onPreExecute() {listView.setRefreshing();}@Overrideprotected void onPostExecute(Object result) {mDatas.add(0, (Map<String, Object>) result);listView.setLastUpdatedLabel("更新时间:" + System.currentTimeMillis());// 通知 adapter 更新,不能少adapter.notifyDataSetChanged();listView.onRefreshComplete();}@Overrideprotected Object doInBackground(Object... params) {try {Thread.sleep(2000);byte[] buffer = loadRawDataFromURL("http://pics.sc.chinaz.com/Files/pic/icons128/5982/s1.png");Bitmap bm =BitmapFactory.decodeByteArray(buffer, 0, buffer.length);map = new HashMap<String, Object>();map.put(KEY_IMAGE, bm);map.put(KEY_TEXT, count++ +"");return map;} catch (InterruptedException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}return null;}}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;}}


1 0