更新LisView里面的进度条

  今天看到论坛上有人问这个。估计是要搞一个类似下载软件的东西吧。

  所以就有了本篇,原理:

  处理线程先给handler发消息,消息中包括进度信息,handler在去更改List的Adapter里面的信息,并通知List更新UI。

  原理很简单,就直接上码了:

001 package hol.test.listprogress;
002   
003 import java.util.ArrayList;
004 import java.util.HashMap;
005 import java.util.List;
006   
007 import android.app.Activity;
008 import android.content.Context;
009 import android.os.Bundle;
010 import android.os.Handler;
011 import android.os.Message;
012 import android.view.LayoutInflater;
013 import android.view.View;
014 import android.view.ViewGroup;
015 import android.widget.BaseAdapter;
016 import android.widget.Button;
017 import android.widget.ListView;
018 import android.widget.ProgressBar;
019 import android.widget.TextView;
020   
021 public class TestListProgress extends Activity {
022     private Button testDo = null;
023     private ListView progressList = null;
024     private List<HashMap<String, Object>> list;
025     private MyAdapter myAdapet;
026     private MyHandler myHandler;
027   
028     @Override
029     public void onCreate(Bundle savedInstanceState) {
030         super.onCreate(savedInstanceState);
031         setContentView(R.layout.main);
032   
033         myHandler = new MyHandler();
034   
035         findView();
036         addListerner();
037         initList();
038     }
039   
040     private void findView() {
041         testDo = (Button) findViewById(R.id.btn_test);
042         progressList = (ListView) findViewById(R.id.lst_progress);
043     }
044   
045     private void addListerner() {
046         testDo.setOnClickListener(new ClickEvent());
047     }
048   
049     private void initList() {
050         list = new ArrayList<HashMap<String, Object>>();
051         HashMap<String, Object> dataA = new HashMap<String, Object>();
052         HashMap<String, Object> dataB = new HashMap<String, Object>();
053         dataA.put("title", "dataA");
054         dataA.put("current", 0);
055         list.add(dataA);
056         dataB.put("title", "dataB");
057         dataB.put("current", 0);
058         list.add(dataB);
059   
060         myAdapet = new MyAdapter(TestListProgress.this, list);
061         progressList.setAdapter(myAdapet);
062   
063     }
064   
065     // 模拟处理线程
066     class UpdateRunnable implements Runnable {
067         int id;
068         int currentPos = 0;
069         int delay;
070         MyHandler handler;
071   
072         public UpdateRunnable(int id, int delay, MyHandler handler) {
073             this.id = id;
074             this.handler = handler;
075             this.delay = delay;
076         }
077   
078         @Override
079         public void run() {
080             // TODO Auto-generated method stub
081             while (currentPos <= 100) {
082                 Message msg = handler.obtainMessage();
083                 msg.what = 1;
084                 msg.arg1 = id;
085                 msg.arg2 = currentPos;
086                 currentPos = currentPos + 5;
087                 msg.sendToTarget();
088                 try {
089                     Thread.sleep(delay);
090                 } catch (InterruptedException e) {
091                     // TODO Auto-generated catch block
092                     e.printStackTrace();
093                 }
094             }
095         }
096   
097     }
098   
099     // 消息Handler用来更新UI
100     class MyHandler extends Handler {
101         @Override
102         public void handleMessage(Message msg) {
103             // TODO Auto-generated method stub
104             switch (msg.what) {
105             case 1:
106                 int id = msg.arg1;
107                 int current = msg.arg2;
108                 updateProgress(id, current);
109                 break;
110             }
111             super.handleMessage(msg);
112         }
113   
114         private void updateProgress(int id, int currentPos) {
115             HashMap<String, Object> dataTemp = list.get(id);
116             dataTemp.put("current", currentPos);
117             myAdapet.chargeProgress(id, dataTemp);
118         }
119     }
120   
121     // 按钮事件
122     class ClickEvent implements View.OnClickListener {
123   
124         @Override
125         public void onClick(View v) {
126             // TODO Auto-generated method stub
127             switch (v.getId()) {
128             case (R.id.btn_test):
129                 new Thread(new UpdateRunnable(0, 400, myHandler)).start();
130                 new Thread(new UpdateRunnable(1, 200, myHandler)).start();
131                 System.out.println("OnClick");
132                 break;
133             }
134         }
135   
136     }
137   
138     // List的显示
139     class MyAdapter extends BaseAdapter {
140   
141         List<HashMap<String, Object>> list;
142         LayoutInflater infl = null;
143   
144         public MyAdapter(Context context, List<HashMap<String, Object>> list) {
145             this.infl = (LayoutInflater) context
146                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
147             this.list = list;
148         }
149   
150         @Override
151         public int getCount() {
152             // TODO Auto-generated method stub
153             return list.size();
154         }
155   
156         @Override
157         public Object getItem(int position) {
158             // TODO Auto-generated method stub
159             return list.get(position);
160         }
161   
162         @Override
163         public long getItemId(int position) {
164             // TODO Auto-generated method stub
165             return position;
166         }
167   
168         @Override
169         public View getView(int position, View convertView, ViewGroup parent) {
170             // TODO Auto-generated method stub
171             convertView = infl.inflate(R.layout.list_layout, null);
172             ProgressBar progressBar = (ProgressBar) convertView
173                     .findViewById(R.id.progress);
174             TextView titleView = (TextView) convertView
175                     .findViewById(R.id.title);
176   
177             HashMap<String, Object> detail = list.get(position);
178             String t = (String) detail.get("title");
179             titleView.setText(t);
180             int progress = (Integer) detail.get("current");
181             progressBar.setProgress(progress);
182             return convertView;
183         }
184   
185         // 改变进度,postion就是要改的那个进度
186         public void chargeProgress(int postion, HashMap<String, Object> detail) {
187             this.list.set(postion, detail);
188             // System.out.println("charged:" + detail.get("current"));
189             notifyDataSetChanged();
190         }
191   
192     }
193 }
MyAdapet 里面的 chargeProgress是在用来更新List里面的值。
Message里面arg1就是List中的第几个进度条,arg2就是这个进度条里面的当前应该有的进度。what暂时没什么意义,只是为了标识这类消息是用来更新进度条的。本例也只有这一类消息。
这个是List的LayoutXML文件内容:

01 <LinearLayout
03   android:orientation="horizontal"
04   android:layout_width="fill_parent"
05   android:layout_height="fill_parent">
06   <TextView  android:id="@+id/title"
07     android:layout_width="wrap_content"
08     android:layout_height="wrap_content"
09     android:text="In list"
10     android:layout_marginRight="5dip"
11     >
12   </TextView>
13   <ProgressBar android:id="@+id/progress"
14     android:layout_width="wrap_content"
15     android:layout_height="wrap_content"
16     style="?android:attr/progressBarStyleHorizontal"
17     android:max="100"
18     android:layout_weight="1.0"
19     android:visibility="visible"
20   />
21 </LinearLayout>
Acitvity的布局:

01 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
02     android:orientation="vertical"
03     android:layout_width="fill_parent"
04     android:layout_height="fill_parent"
05     >
06 <TextView  
07     android:layout_width="fill_parent" 
08     android:layout_height="wrap_content" 
09     android:text="@string/hello"
10     />
11     <Button android:id="@+id/btn_test"
12         android:layout_width="fill_parent"
13         android:layout_height="wrap_content"
14         android:text="ListProgress"
15     />
16     <ListView android:id="@+id/lst_progress"
17         android:layout_width="fill_parent"
18         android:layout_height="wrap_content"
19         android:layout_weight="1.0"
20         >
21     </ListView>
22 </LinearLayout>
效果图如下:

posted on 2011-08-01 16:07  一叶飘舟  阅读(567)  评论(0编辑  收藏  举报