今天看到论坛上有人问这个。估计是要搞一个类似下载软件的东西吧。
所以就有了本篇,原理:
处理线程先给handler发消息,消息中包括进度信息,handler在去更改List的Adapter里面的信息,并通知List更新UI。
原理很简单,就直接上码了:
001 |
package hol.test.listprogress; |
003 |
import java.util.ArrayList; |
004 |
import java.util.HashMap; |
005 |
import java.util.List; |
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; |
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; |
029 |
public void onCreate(Bundle savedInstanceState) { |
030 |
super .onCreate(savedInstanceState); |
031 |
setContentView(R.layout.main); |
033 |
myHandler = new MyHandler(); |
040 |
private void findView() { |
041 |
testDo = (Button) findViewById(R.id.btn_test); |
042 |
progressList = (ListView) findViewById(R.id.lst_progress); |
045 |
private void addListerner() { |
046 |
testDo.setOnClickListener( new ClickEvent()); |
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 ); |
056 |
dataB.put( "title" , "dataB" ); |
057 |
dataB.put( "current" , 0 ); |
060 |
myAdapet = new MyAdapter(TestListProgress. this , list); |
061 |
progressList.setAdapter(myAdapet); |
066 |
class UpdateRunnable implements Runnable { |
072 |
public UpdateRunnable( int id, int delay, MyHandler handler) { |
074 |
this .handler = handler; |
081 |
while (currentPos <= 100 ) { |
082 |
Message msg = handler.obtainMessage(); |
085 |
msg.arg2 = currentPos; |
086 |
currentPos = currentPos + 5 ; |
090 |
} catch (InterruptedException e) { |
100 |
class MyHandler extends Handler { |
102 |
public void handleMessage(Message msg) { |
107 |
int current = msg.arg2; |
108 |
updateProgress(id, current); |
111 |
super .handleMessage(msg); |
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); |
122 |
class ClickEvent implements View.OnClickListener { |
125 |
public void onClick(View v) { |
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" ); |
139 |
class MyAdapter extends BaseAdapter { |
141 |
List<HashMap<String, Object>> list; |
142 |
LayoutInflater infl = null ; |
144 |
public MyAdapter(Context context, List<HashMap<String, Object>> list) { |
145 |
this .infl = (LayoutInflater) context |
146 |
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
151 |
public int getCount() { |
157 |
public Object getItem( int position) { |
159 |
return list.get(position); |
163 |
public long getItemId( int position) { |
169 |
public View getView( int position, View convertView, ViewGroup parent) { |
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); |
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); |
186 |
public void chargeProgress( int postion, HashMap<String, Object> detail) { |
187 |
this .list.set(postion, detail); |
189 |
notifyDataSetChanged(); |
MyAdapet 里面的 chargeProgress是在用来更新List里面的值。 |
Message里面arg1就是List中的第几个进度条,arg2就是这个进度条里面的当前应该有的进度。what暂时没什么意义,只是为了标识这类消息是用来更新进度条的。本例也只有这一类消息。 |
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" |
13 |
< ProgressBar android:id = "@+id/progress" |
14 |
android:layout_width = "wrap_content" |
15 |
android:layout_height = "wrap_content" |
16 |
style = "?android:attr/progressBarStyleHorizontal" |
18 |
android:layout_weight = "1.0" |
19 |
android:visibility = "visible" |
02 |
android:orientation= "vertical" |
03 |
android:layout_width= "fill_parent" |
04 |
android:layout_height= "fill_parent" |
07 |
android:layout_width= "fill_parent" |
08 |
android:layout_height= "wrap_content" |
09 |
android:text= "@string/hello" |
11 |
<Button android:id= "@+id/btn_test" |
12 |
android:layout_width= "fill_parent" |
13 |
android:layout_height= "wrap_content" |
14 |
android:text= "ListProgress" |
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" |