使用自訂的item、Adapter和AsyncTask、第三方開源架構PullToRefresh聯合使用實現自訂的下拉式清單(從網路載入圖片顯示在item中的ImageView),
AsyncTask使用方法詳情:http://www.cnblogs.com/zzw1994/p/4959949.html
下拉開源架構PullToRefresh使用方法和下載詳情:http://www.cnblogs.com/zzw1994/p/4992194.html
具體實現的代碼如下:
item.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 > 6 7 <ImageView 8 android:id="@+id/imageView1" 9 android:layout_width="50dp"10 android:layout_height="50dp"11 android:layout_alignParentLeft="true"12 android:src="@drawable/ic_launcher" />13 14 <TextView15 android:id="@+id/textView"16 android:layout_width="wrap_content"17 android:layout_height="wrap_content"18 android:layout_alignBottom="@+id/imageView1"19 android:layout_alignParentTop="true"20 android:layout_toLeftOf="@+id/imageView2"21 android:layout_toRightOf="@+id/imageView1"22 android:gravity="center"23 android:text="TextView"24 android:textColor="@android:color/holo_red_light"25 android:textSize="30sp" />26 27 <ImageView28 android:id="@+id/imageView2"29 android:layout_width="50dp"30 android:layout_height="50dp"31 android:layout_alignParentRight="true"32 android:layout_alignParentTop="true"33 android:src="@drawable/ic_launcher" />34 35 </RelativeLayout>item.xml
activity_main.xml:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="com.zzw.testpulltorefresh.MainActivity" > 6 7 <com.handmark.pulltorefresh.library.PullToRefreshListView 8 android:id="@+id/listView" 9 android:layout_width="match_parent"10 android:layout_height="match_parent" />11 12 </RelativeLayout>activity_main.xml
MainActivity.java:
1 package com.zzw.testpulltorefresh; 2 3 import java.io.BufferedInputStream; 4 import java.io.ByteArrayOutputStream; 5 import java.io.InputStream; 6 import java.net.HttpURLConnection; 7 import java.net.URL; 8 import java.util.ArrayList; 9 import java.util.Date; 10 import java.util.HashMap; 11 12 import com.handmark.pulltorefresh.library.PullToRefreshBase; 13 import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode; 14 import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener; 15 import com.handmark.pulltorefresh.library.PullToRefreshListView; 16 17 import android.app.Activity; 18 import android.content.Context; 19 import android.graphics.Bitmap; 20 import android.graphics.BitmapFactory; 21 import android.graphics.Color; 22 import android.os.AsyncTask; 23 import android.os.Bundle; 24 import android.view.LayoutInflater; 25 import android.view.View; 26 import android.view.ViewGroup; 27 import android.widget.ArrayAdapter; 28 import android.widget.ImageView; 29 import android.widget.ListView; 30 import android.widget.TextView; 31 import android.widget.Toast; 32 33 public class MainActivity extends Activity { 34 private int COUNT = 0; 35 36 private final String IMAGE_KEY1 = "IMAGE_KEY1"; 37 private final String IMAGE_KEY2 = "IMAGE_KEY2"; 38 private final String TEXT_KEY = "TEXT_KEY"; 39 40 private PullToRefreshListView listView; 41 private ArrayList<HashMap<String, Object>> data; 42 private ArrayAdapter adapter; 43 private String[] urls = { "http://p1.so.qhimg.com/bdr/_240_/t01829c584b50b68311.jpg", 44 "http://p2.so.qhimg.com/bdr/_240_/t011c2cd4fe8723bcb2.jpg", 45 "http://p4.so.qhimg.com/bdr/_240_/t01b11db3c0961b24a9.jpg", 46 "http://p4.so.qhimg.com/bdr/_240_/t019786092c7b1688b9.jpg", 47 "http://p4.so.qhimg.com/bdr/_240_/t015b226d64a10097ce.jpg", 48 "http://p1.so.qhimg.com/bdr/_240_/t01f6c4382c907133ab.jpg" }; 49 50 @Override 51 protected void onCreate(Bundle savedInstanceState) { 52 super.onCreate(savedInstanceState); 53 setContentView(R.layout.activity_main); 54 55 data = new ArrayList<HashMap<String, Object>>(); 56 57 listView = (PullToRefreshListView) findViewById(R.id.listView); 58 59 // 設定向下滑動時重新整理 60 listView.setMode(Mode.PULL_FROM_START); 61 // 支援下拉和上拉 listView.setMode(Mode.BOTH); 62 63 // 設定監聽 64 listView.setOnRefreshListener(new OnRefreshListener<ListView>() { 65 66 @Override 67 public void onRefresh(PullToRefreshBase<ListView> refreshView) { 68 // 在這完成商務邏輯 69 new MyAsyncTask().execute(); 70 } 71 }); 72 73 adapter = new MyAdapter(this, R.layout.item); 74 listView.setAdapter(adapter); 75 76 // 設定如果資料為空白的時候顯示什麼 77 TextView textView = new TextView(this); 78 textView.setText("請下拉重新整理"); 79 listView.setEmptyView(textView); 80 } 81 82 //自訂的Item顯示參數設定在Adapter中操作 83 private class MyAdapter extends ArrayAdapter { 84 85 private LayoutInflater inflater; 86 private int item; 87 88 public MyAdapter(Context context, int resource) { 89 super(context, resource); 90 inflater = LayoutInflater.from(context); 91 this.item = resource; 92 } 93 94 @Override 95 public int getCount() { 96 return data.size(); 97 } 98 99 @Override100 public HashMap<String, Object> getItem(int position) {101 102 return data.get(position);103 }104 105 @Override106 public View getView(int position, View convertView, ViewGroup parent) {107 HashMap<String, Object> map = getItem(position);108 109 if (convertView == null) {110 convertView = inflater.inflate(item, null);111 }112 113 ImageView image1 = (ImageView) convertView.findViewById(R.id.imageView1);114 ImageView image2 = (ImageView) convertView.findViewById(R.id.imageView2);115 TextView textView = (TextView) convertView.findViewById(R.id.textView);116 117 image1.setImageBitmap((Bitmap) map.get(IMAGE_KEY1));118 image2.setImageBitmap((Bitmap) map.get(IMAGE_KEY2));119 textView.setText(map.get(TEXT_KEY) + "");120 121 if (position % 2 == 1) {122 textView.setTextColor(Color.BLUE);123 }124 125 return convertView;126 }127 128 }129 130 131 //連網等一系列延時操作在AsyncTask中操作132 private class MyAsyncTask extends AsyncTask {133 134 @Override135 protected void onPreExecute() {136 // 開始重新整理137 listView.setRefreshing();138 }139 140 @Override141 protected Object doInBackground(Object... params) {142 HashMap<String, Object> map = new HashMap<String, Object>();143 try {144 byte[] buf = loadRawDataFromURL(urls[COUNT]);145 BitmapFactory factory = new BitmapFactory();146 Bitmap bitmap = factory.decodeByteArray(buf, 0, buf.length);147 map.put(TEXT_KEY, "資料->" + COUNT);148 map.put(IMAGE_KEY1, bitmap);149 map.put(IMAGE_KEY2, bitmap);150 } catch (Exception e) {151 e.printStackTrace();152 }153 return map;154 }155 156 @Override157 protected void onPostExecute(Object result) {158 159 data.add(0,(HashMap<String, Object>) result);160 adapter.notifyDataSetChanged();161 162 // 設定標籤163 listView.setLastUpdatedLabel("最後更新新的時間:" + new Date());164 165 // 重新整理完成166 listView.onRefreshComplete();167 COUNT++;168 if (COUNT >= urls.length) {169 COUNT = 0;170 }171 Toast.makeText(getApplicationContext(), "載入成功", 0).show();172 }173 }174 175 // 通過URL讀取位元組數組176 public static byte[] loadRawDataFromURL(String u) throws Exception {177 URL url = new URL(u);178 HttpURLConnection conn = (HttpURLConnection) url.openConnection();179 180 InputStream is = conn.getInputStream();181 BufferedInputStream bis = new BufferedInputStream(is);182 183 ByteArrayOutputStream baos = new ByteArrayOutputStream();184 // 緩衝2KB185 final int BUFFER_SIZE = 2 * 1024;186 final int EOF = -1;187 188 int c;189 byte[] buf = new byte[BUFFER_SIZE];190 191 while (true) {192 c = bis.read(buf);193 if (c == EOF)194 break;195 196 baos.write(buf, 0, c);197 }198 199 conn.disconnect();200 is.close();201 202 byte[] b = baos.toByteArray();203 baos.flush();204 205 return b;206 }207 208 }