編輯:Android開發實例
今天看到論壇上有人問這個。估計是要搞一個類似下載軟件的東西吧。
所以就有了本篇,原理:
處理線程先給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
02
xmlns:android
=
"http://schemas.android.com/apk/res/android"
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>
效果圖如下:
初學Android編程,Android SDK中提供的Sample代碼自然是最好的學習材料。 &nb
Android中的翻轉動畫效果的實現,首先看一下運行效果如上圖所示. Android中並沒有提供直接做3D翻轉的動畫,所以關於3D翻轉的動畫效果需要我們自己實現,
JSON代表JavaScript對象符號。它是一個獨立的數據交換格式,是XML的最佳替代品。本章介紹了如何解析JSON文件,並從中提取所需的信息。Android提供了四個
Android應用程序可以在許多不同地區的許多設備上運行。為了使應用程序更具交互性,應用程序應該處理以適合應用程序將要使用的語言環境方面的文字,數字,文件等。在本章中,我