Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 自己定制ListView,上拉刷新和下拉刷新,加載網絡圖片,並且添加緩存機制。,自己定制listview

自己定制ListView,上拉刷新和下拉刷新,加載網絡圖片,並且添加緩存機制。,自己定制listview

編輯:關於android開發

自己定制ListView,上拉刷新和下拉刷新,加載網絡圖片,並且添加緩存機制。,自己定制listview


  1 package com.lixu.listviewrefresh;
  2 
  3 import java.util.ArrayList;
  4 import java.util.HashMap;
  5 import java.util.Random;
  6 
  7 import com.lixu.listviewrefresh.Loadnetimage.OnLoadnetimageListener;
  8 import com.lixu.listviewrefresh.MyRefreshListview.OnRefreshListener;
  9 import android.annotation.SuppressLint;
 10 import android.app.Activity;
 11 import android.app.ProgressDialog;
 12 import android.content.Context;
 13 import android.graphics.Bitmap;
 14 import android.graphics.BitmapFactory;
 15 import android.os.AsyncTask;
 16 import android.os.Bundle;
 17 import android.util.Log;
 18 import android.view.LayoutInflater;
 19 import android.view.View;
 20 import android.view.ViewGroup;
 21 import android.widget.ArrayAdapter;
 22 import android.widget.ImageView;
 23 import android.widget.ListView;
 24 import android.widget.TextView;
 25 
 26 public class MainActivity extends Activity {
 27     private static final String KEY_IMAGE = "image_key";
 28     private static final String KEY_TEXT = "text_key";
 29 
 30     private static final String CACHE_KEY_IMAGE = "cache_image_key";
 31     private static final String CACHE_KEY_TEXT = "cache_text_key";
 32     // 原始數據
 33     private ArrayList<HashMap<String, Object>> data;
 34     // 緩存隊列
 35     private ArrayList<HashMap<String, Object>> cache;
 36 
 37     private ArrayAdapter<String> mMyAdapter;
 38 
 39     private MyRefreshListview lv;
 40 
 41     private Activity activity;
 42     private Random random = new Random();
 43     // 圖片網絡地址
 44     private String[] image_urls = { "http://img0.bdstatic.com/img/image/shouye/mxh007.jpg",
 45             "http://f.hiphotos.baidu.com/image/h%3D360/sign=1c9a50843ec79f3d90e1e2368aa0cdbc/f636afc379310a5566becb8fb24543a982261036.jpg",
 46             "http://h.hiphotos.baidu.com/image/h%3D360/sign=04ef437b0b23dd543e73a16ee108b3df/50da81cb39dbb6fd9d9ada390b24ab18962b37ef.jpg",
 47             "http://b.hiphotos.baidu.com/image/h%3D360/sign=e91921f7c1fdfc03fa78e5bee43e87a9/8b82b9014a90f60352684b4e3b12b31bb151eddb.jpg",
 48             "http://g.hiphotos.baidu.com/image/h%3D360/sign=a2540bdbf21f3a2945c8d3c8a925bce3/fd039245d688d43fa9943bf77f1ed21b0ef43bf1.jpg",
 49             "http://p4.so.qhimg.com/bdr/_240_/t01d4c378e7c6bfbe69.jpg",
 50             "http://p0.so.qhimg.com/bdr/_240_/t0160872a7d33df5735.jpg",
 51             "http://p3.so.qhimg.com/bdr/_240_/t01473a131702c357dd.jpg" };
 52 
 53     @Override
 54     protected void onCreate(Bundle savedInstanceState) {
 55         super.onCreate(savedInstanceState);
 56         setContentView(R.layout.activity_main);
 57 
 58         activity = this;
 59 
 60         data = new ArrayList<HashMap<String, Object>>();
 61         cache = new ArrayList<HashMap<String, Object>>();
 62         // 添加初始數據
 63         for (int i = 0; i < 30; i++) {
 64             HashMap<String, Object> map = new HashMap<String, Object>();
 65             map.put(KEY_IMAGE, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
 66             map.put(KEY_TEXT, i);
 67             data.add(map);
 68         }
 69 
 70         lv = (MyRefreshListview) findViewById(R.id.listview);
 71 
 72         mMyAdapter = new MyAdapter(this, -1);
 73 
 74         lv.setAdapter(mMyAdapter);
 75 
 76         lv.setOnRefreshListener(new OnRefreshListener() {
 77 
 78             @Override
 79             public void ontop() {
 80                 // 隨機加入一張圖片
 81                 adddata(image_urls[random.nextInt(8)]);
 82 
 83             }
 84 
 85             @Override
 86             public void onbuttom() {
 87                 // 沒有設置緩存機制
 88                 new MyAsyncTaskBottom(image_urls[random.nextInt(8)]).execute();
 89             }
 90         });
 91     }
 92 
 93     private class MyAdapter extends ArrayAdapter<String> {
 94         private LayoutInflater flater;
 95         private Context context;
 96 
 97         public MyAdapter(Context context, int resource) {
 98 
 99             super(context, resource);
100             this.context = context;
101             flater = LayoutInflater.from(context);
102 
103         }
104 
105         @Override
106         public int getCount() {
107             return data.size();
108         }
109 
110         @SuppressLint("InflateParams")
111         @Override
112         public View getView(int position, View convertView, ViewGroup parent) {
113 
114             if (convertView == null)
115                 convertView = flater.inflate(R.layout.list, null);
116 
117             ImageView iv = (ImageView) convertView.findViewById(R.id.image);
118 
119             iv.setImageBitmap((Bitmap) data.get(position).get(KEY_IMAGE));
120 
121             TextView tv = (TextView) convertView.findViewById(R.id.text);
122 
123             tv.setText(position + "原始數據");
124 
125             return convertView;
126         }
127 
128     }
129 
130     private class MyAsyncTaskTop extends AsyncTask {
131         private ProgressDialog pd;
132         @SuppressWarnings("unused")
133         private String url;
134 
135         public MyAsyncTaskTop(String url) {
136             this.url = url;
137         }
138 
139         @Override
140         protected void onPreExecute() {
141             // 設置加載進度條
142             pd = new ProgressDialog(activity);
143             pd.setProgressStyle(pd.STYLE_HORIZONTAL);
144             pd.setMessage("正在加載。。。");
145             pd.show();
146         }
147 
148         @Override
149         protected Object doInBackground(Object... params) {
150 
151             try {
152                 // 回調加載網絡的類Loadnetimage
153                 Loadnetimage mLoadnetimage = new Loadnetimage();
154 
155                 mLoadnetimage.SetLoadnetimageListener(new OnLoadnetimageListener() {
156 
157                     @SuppressWarnings("unchecked")
158                     @Override
159                     public void LoadnetimageListener(int count, int total) {
160                         publishProgress(count, total);
161 
162                     }
163                 });
164                 // 獲取網絡圖片
165                 Bitmap bitmap = mLoadnetimage.loadRawDataFromURL(url);
166                 // 每次將圖片和對應的地址放入HashMap
167                 HashMap<String, Object> map = new HashMap<String, Object>();
168                 // 將新加載圖片放入緩存
169                 map.put(CACHE_KEY_IMAGE, bitmap);
170                 map.put(CACHE_KEY_TEXT, url);
171 
172                 cache.add(map);
173 
174                 return bitmap;
175             } catch (Exception e) {
176                 e.printStackTrace();
177             }
178 
179             return null;
180 
181         }
182 
183         @Override
184         protected void onPostExecute(Object result) {
185             addtop(result);
186             pd.dismiss();
187 
188         }
189 
190         // 更新進度條
191         @Override
192         protected void onProgressUpdate(Object... values) {
193             int count = (Integer) values[0];
194             int total = (Integer) values[1];
195 
196             pd.setMax(total);
197             pd.setProgress(count);
198 
199         }
200 
201     }
202 
203     @SuppressWarnings("unused")
204     private class MyAsyncTaskBottom extends AsyncTask {
205         private ProgressDialog pd;
206         private String url;
207 
208         public MyAsyncTaskBottom(String url) {
209             this.url = url;
210         }
211 
212         @Override
213         protected void onPreExecute() {
214             // 設置進度條
215             pd = new ProgressDialog(activity);
216             // 樣式
217             pd.setProgressStyle(pd.STYLE_HORIZONTAL);
218             pd.setMessage("正在加載。。。");
219             pd.show();
220 
221         }
222 
223         @Override
224         protected Object doInBackground(Object... params) {
225             // 回調加載網絡的類Loadnetimage
226             Loadnetimage mLoadnetimage = new Loadnetimage();
227             // 獲取網絡圖片
228             Bitmap bitmap;
229             try {
230                 bitmap = mLoadnetimage.loadRawDataFromURL(url);
231                 return bitmap;
232             } catch (Exception e) {
233                 e.printStackTrace();
234             }
235             return null;
236         }
237 
238         @Override
239         protected void onPostExecute(Object result) {
240             addbottom(result);
241             pd.dismiss();
242 
243         }
244 
245     }
246 
247     private void adddata(String url) {
248         // 設置緩存機制
249         Log.e("", "開始添加");
250 
251         for (int i = 0; i < cache.size(); i++) {
252 
253             HashMap<String, Object> map = cache.get(i);
254             // 判斷圖片地址一樣則載入緩存
255             if (url.equals(map.get(CACHE_KEY_TEXT))) {
256                 Bitmap bitmap = (Bitmap) map.get(CACHE_KEY_IMAGE);
257                 addtop(bitmap);
258                 Log.e("", "載入緩存");
259 
260                 return;
261             }
262 
263         }
264         // 否則從網絡新加載
265         Log.e("", "最新加載");
266         new MyAsyncTaskTop(url).execute();
267 
268     }
269 
270     private void addtop(Object result) {
271 
272         // 新加載圖片放入原始數據
273         HashMap<String, Object> map = new HashMap<String, Object>();
274         map.put(KEY_IMAGE, result);
275         map.put(KEY_TEXT, data.size() + 1);
276 
277         data.add(0, map);
278 
279         mMyAdapter.notifyDataSetChanged();
280     }
281 
282     private void addbottom(Object result) {
283         HashMap<String, Object> map = new HashMap<String, Object>();
284         map.put(KEY_IMAGE, result);
285         map.put(KEY_TEXT, data.size() + 1);
286 
287         data.add(map);
288 
289         mMyAdapter.notifyDataSetChanged();
290         // 添加數據後自動滾動到底部
291         lv.setSelection(ListView.FOCUS_DOWN);
292 
293     }
294 
295 }
 1 package com.lixu.listviewrefresh;
 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 
 9 import android.graphics.Bitmap;
10 import android.graphics.BitmapFactory;
11 
12 public class Loadnetimage {
13     // 設置回調機制
14     
15     private OnLoadnetimageListener mOnLoadnetimageListener;
16 
17     public Loadnetimage() {
18 
19     }
20 
21     public interface OnLoadnetimageListener {
22         public void LoadnetimageListener(int count, int total);
23     }
24 
25     public void SetLoadnetimageListener(OnLoadnetimageListener l) {
26         mOnLoadnetimageListener = l;
27     }
28     
29     
30 
31     public Bitmap loadRawDataFromURL(String u) throws Exception {
32         
33         URL url = new URL(u);
34         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
35         // 設置請求的一些常用的參數
36         conn.setReadTimeout(30000);
37         conn.setConnectTimeout(30000);
38         conn.setDoInput(true);
39         conn.connect();
40         // 獲取圖片總數
41         int total = conn.getContentLength();
42 
43         InputStream is = conn.getInputStream();
44         BufferedInputStream bis = new BufferedInputStream(is);
45 
46         ByteArrayOutputStream baos = new ByteArrayOutputStream();
47         // 緩存2KB
48         final int BUFFER_SIZE = 2 * 1024;
49         final int EOF = -1;
50 
51         int count = 0;
52 
53         int c;
54         byte[] buf = new byte[BUFFER_SIZE];
55 
56         while (true) {
57             c = bis.read(buf);
58             if (c == EOF)
59                 break;
60             // 每次累加到現在為止我們下載了多少數據,以便於後面計算已經下載的數據占了總數量的百分比
61             count = count + c;
62             // 發布最新的數據,更新隨後的進度條顯示進度使用
63             if (mOnLoadnetimageListener != null)
64                 mOnLoadnetimageListener.LoadnetimageListener(count, total);
65             baos.write(buf, 0, c);
66         }
67 
68         conn.disconnect();
69         is.close();
70 
71         byte[] data = baos.toByteArray();
72         baos.flush();
73 
74         Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
75 
76         return bitmap;
77     }
78 
79 }
 1 package com.lixu.listviewrefresh;
 2 
 3 import android.content.Context;
 4 import android.util.AttributeSet;
 5 import android.view.GestureDetector;
 6 import android.view.MotionEvent;
 7 import android.view.View;
 8 import android.widget.AbsListView;
 9 import android.widget.ListView;
10 
11 public class MyRefreshListview extends ListView {
12     private OnRefreshListener mOnRefreshListener;
13     private Context context;
14     private int firstVisibleItem, visibleItemCount, totalItemCount;
15 
16     public MyRefreshListview(Context context, AttributeSet attrs) {
17         super(context, attrs);
18         this.context = context;
19 
20     }
21 
22     public interface OnRefreshListener {
23 
24         public void ontop();
25 
26         public void onbuttom();
27     }
28 
29     public void setOnRefreshListener(OnRefreshListener l) {
30         this.mOnRefreshListener = l;
31         // 這個方法可以解決當listview中初始沒有數據的時候滑動來加載數據
32         final GestureDetector mGestureDetector = new GestureDetector(context,
33                 new GestureDetector.SimpleOnGestureListener() {
34 
35                     // velocityY表示觸摸停止點Y到觸摸起始點Y的速度值 當兩點距離值為負時,速度值也為負所以:
36                     // >0表示從上往下拉
37                     // <0表示從下往上拉
38                     public boolean onFling(android.view.MotionEvent e1, android.view.MotionEvent e2, float velocityX,
39                             float velocityY) {
40                         if (velocityY > 0) {
41 
42                             // 滑動到頂部時添加
43                             if (firstVisibleItem == 0) {
44 
45                                 mOnRefreshListener.ontop();
46 
47                             }
48 
49                         }
50                         if (velocityY < 0) {
51                             // 滑動到底部時添加
52                             if ((firstVisibleItem + visibleItemCount) == totalItemCount) {
53 
54                                 mOnRefreshListener.onbuttom();
55 
56                             }
57                         }
58 
59                         return super.onFling(e1, e2, velocityX, velocityY);
60                     };
61 
62                 });
63         this.setOnTouchListener(new OnTouchListener() {
64             // 將觸摸事件交給GestureDetector來處理
65             @Override
66             public boolean onTouch(View v, MotionEvent event) {
67 
68                 return mGestureDetector.onTouchEvent(event);
69             }
70         });
71 
72         this.setOnScrollListener(new OnScrollListener() {
73 
74             @Override
75             public void onScrollStateChanged(AbsListView view, int scrollState) {
76 
77             }
78 
79             @Override
80             public void onScroll(AbsListView view, int fvi, int vic, int tic) {
81 
82                 firstVisibleItem = fvi;
83                 visibleItemCount = vic;
84                 totalItemCount = tic;
85 
86             }
87         });
88 
89     }
90 
91 }

 

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.lixu.listviewrefresh.MainActivity" >
 6 
 7     <com.lixu.listviewrefresh.MyRefreshListview
 8         android:id="@+id/listview"
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent" />
11 
12 </RelativeLayout>
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <ImageView
 8         android:id="@+id/image"
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent" />
11 
12     <TextView
13         android:id="@+id/text"
14         android:layout_width="match_parent"
15         android:layout_height="match_parent" />
16 
17 </LinearLayout>

運行效果圖:

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved