main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="用户列表" android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="fill_parent" android:background="#DAA520"
android:textColor="#000000">
</TextView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="姓名"
android:gravity="center" android:layout_width="160px"
android:layout_height="wrap_content" android:textStyle="bold"
android:background="#7CFC00">
</TextView>
<TextView android:text="年龄"
android:layout_width="170px" android:gravity="center"
android:layout_height="wrap_content" android:textStyle="bold"
android:background="#F0E68C">
</TextView>
</LinearLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/users">
</ListView>
</LinearLayout>
user.xml<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
>
<TableRow >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img">
</ImageView>
<TextView
android:layout_height="wrap_content"
android:layout_width="150px"
android:id="@+id/name">
</TextView>
<TextView
android:layout_height="wrap_content"
android:layout_width="170px"
android:id="@+id/age">
</TextView>
</TableRow>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="5dip"
android:max="100"
android:progress="10"
android:id="@+id/progressBar"
/>
</LinearLayout>
</TableLayout> Activitypackage com.list;import java.util.ArrayList;
import java.util.HashMap;import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;public class ListTest extends Activity {
/** Called when the activity is first created. */ @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, Object>> users = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < 10; i++) {
HashMap<String, Object> user = new HashMap<String, Object>();
user.put("img", R.drawable.user);
user.put("username", "姓名(" + i + ")");
user.put("age", (20 + i) + "");
users.add(user);
}
SimpleAdapter saImageItems = new SimpleAdapter(this, users,// 数据来源
R.layout.user,// 每一个user xml 相当ListView的一个组件
new String[] { "img", "username", "age" },
// 分别对应view 的id
new int[] { R.id.img, R.id.name, R.id.age });
// 获取listview
((ListView) findViewById(R.id.users)).setAdapter(saImageItems); }
}我现在想实时更新滚条的显示进度,在网上查了很多,说需要重写adpter之类的,但是我重写了也不会用,还需要handler传递消息,来刷新当前进度条的进度显示,期待牛人的帮助。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="用户列表" android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="fill_parent" android:background="#DAA520"
android:textColor="#000000">
</TextView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="姓名"
android:gravity="center" android:layout_width="160px"
android:layout_height="wrap_content" android:textStyle="bold"
android:background="#7CFC00">
</TextView>
<TextView android:text="年龄"
android:layout_width="170px" android:gravity="center"
android:layout_height="wrap_content" android:textStyle="bold"
android:background="#F0E68C">
</TextView>
</LinearLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/users">
</ListView>
</LinearLayout>
user.xml<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
>
<TableRow >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img">
</ImageView>
<TextView
android:layout_height="wrap_content"
android:layout_width="150px"
android:id="@+id/name">
</TextView>
<TextView
android:layout_height="wrap_content"
android:layout_width="170px"
android:id="@+id/age">
</TextView>
</TableRow>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="5dip"
android:max="100"
android:progress="10"
android:id="@+id/progressBar"
/>
</LinearLayout>
</TableLayout> Activitypackage com.list;import java.util.ArrayList;
import java.util.HashMap;import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;public class ListTest extends Activity {
/** Called when the activity is first created. */ @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, Object>> users = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < 10; i++) {
HashMap<String, Object> user = new HashMap<String, Object>();
user.put("img", R.drawable.user);
user.put("username", "姓名(" + i + ")");
user.put("age", (20 + i) + "");
users.add(user);
}
SimpleAdapter saImageItems = new SimpleAdapter(this, users,// 数据来源
R.layout.user,// 每一个user xml 相当ListView的一个组件
new String[] { "img", "username", "age" },
// 分别对应view 的id
new int[] { R.id.img, R.id.name, R.id.age });
// 获取listview
((ListView) findViewById(R.id.users)).setAdapter(saImageItems); }
}我现在想实时更新滚条的显示进度,在网上查了很多,说需要重写adpter之类的,但是我重写了也不会用,还需要handler传递消息,来刷新当前进度条的进度显示,期待牛人的帮助。
源码里例子很多
如果LZ愿意参考学习的话,可以看看输入短信彩信内容界面的实现……
ArrayList<HashMap<String, Object>> users = new ArrayList<HashMap<String, Object>>();
SimpleAdapter saImageItems = new SimpleAdapter(this, users,// 数据来源
R.layout.user,// 每一个user xml 相当ListView的一个组件
new String[] { "img", "username", "age" },
// 分别对应view 的id
new int[] { R.id.img, R.id.name, R.id.age });
// 获取listview
((ListView) findViewById(R.id.users)).setAdapter(saImageItems); for (int i = 0; i < 10; i++) {
HashMap<String, Object> user = new HashMap<String, Object>();
user.put("img", R.drawable.user);
user.put("username", "姓名(" + i + ")");
user.put("age", (20 + i) + "");
users.add(user);
mHandler.sendEmptyMessage(10);
}
private Handler mHandler= new Handler(){ @Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 10) {
saImageItems .notifyDataSetChanged();
}
}};
}这样应该就可以动态刷新了,只是你的for里面执行的速度太快,可能效果不明显,再和网络打交道的时候,效果就明显了。
不过SimpleAdapter用着很别扭,可以直接用BaseAdapter,然后在构造函数或者getView()里面直接设置ProgressBar的监听,如果是外因引起的ProgressBar变化,可以发送消息来通知ProgressBar。
1、定义回调接口,比如DownloadListener,里面添加onDownLoadProgress(int size);
2、可以自定义view extends ProgressBar,实现DownloadListener监听回调下载状态
3、下载的话一般是异步线程,在下载管理器里面定义DownloadListener,外部传入,实时回调;
4、自定义ProgressBar目前就可以实时接收到下载的进度,使用自己的Handler给自己发送Message(因为接口回调在非UI线程)来完成更新。
大概思路就是如此,细节无需多说,懂的自然明白,关于第2步也可以直接extends列表的布局Layout,道理一样。