今天继续Android学习之旅之ListView的记录,主要包括简单的列表、类似QQ消息列表的复杂列表以及两者的点击事件。
1.简单的列表
最简单的ListView就是列表里仅有单列字符,实现方式类似于下拉框,也是三步:1.获得数据列表;2.填充数据适配器;3.设置ListView的适配器;
设计页面控件代码:
<ListView
android:id="@+id/listCity"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
后台代码:
private String data[] = new String[] { "江苏", "浙江", "上海" };
private ListView listCity = null;
private ListAdapter arrayAdapter = null;
// 简单的显示城市信息的listView
this.listCity = (ListView) super.findViewById(R.id.listCity);
this.arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1, this.data);
this.listCity.setAdapter(arrayAdapter);
2.复杂列表
类似QQ消息列表的复杂列表,首先看如下图QQ的消息页的截图,左边是头像,右边是上下结构的,上面分别显示昵称和消息的时间,下面是最近的消息内容。<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/headImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/nickName"
android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/recentMsgTime"
android:layout_gravity="right"
android:layout_marginLeft="350px"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:id="@+id/recentMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
后台代码如下:
private int[] headImage = new int[] { R.drawable.first, R.drawable.second };
private String[][] userInfoData = new String[][] {
{ "喜乐", "18:05", "忙碌的一天结束了,同志们该下班啦~" },
{ "和顺", "昨天", "明天记得带上伞!预报的是雷阵雨" } };
private List<Map<String, String>> list = new ArrayList<Map<String, String>>();
private ListView listUserInfo = null;
private SimpleAdapter simpleAdapter = null;
// 显示复杂的ListView
listUserInfo = (ListView) super.findViewById(R.id.listUserInfo);
for (int i = 0, length = userInfoData.length; i < length; i++) {
Map<String, String> map = new HashMap<String, String>();
map.put("headImage", String.valueOf(headImage[i]));
map.put("nickName", userInfoData[i][0]);
map.put("recentMsgTime", userInfoData[i][1]);
map.put("recentMsg", userInfoData[i][2]);
list.add(map);
}
this.simpleAdapter = new SimpleAdapter(this,
list,
R.layout.userinfo_template,
new String[] { "headImage","nickName", "recentMsgTime", "recentMsg" },
new int[] {R.id.headImage, R.id.nickName, R.id.recentMsgTime,R.id.recentMsg });
this.listUserInfo.setAdapter(simpleAdapter);
3.选择事件
选择事件只要订阅OnItemClickListener事件即可,事件代码如下:
private class OnItemClickListenerImplSample implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String selectInfo = MainActivity.this.arrayAdapter
.getItem(position).toString();
Toast.makeText(MainActivity.this, "选择的城市是:" + selectInfo,
Toast.LENGTH_LONG).show();
}
}
private class OnItemClickListenerImpl implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Map<String, String> map = (Map<String, String>) MainActivity.this
.simpleAdapter.getItem(position);
String nickName = map.get("nickName");
String recentMsgTime = map.get("recentMsgTime");
String recentMsg = map.get("recentMsg");
String selectInfo = "nickName:" + nickName + ",recentMsgTime:"
+ recentMsgTime + ",recentMsg:" + recentMsg;
Toast.makeText(MainActivity.this, selectInfo, Toast.LENGTH_LONG)
.show();
}
}
项目运行结果(跟QQ的还是差别很大的,只是简单的布局类似):