android listview怎么加载自定义布局,还必须是在代码里面写
前面总结了如何应用android自带的布局到ListView中去,现在如果想让自己的ListView更生动,只需要将自己定义的布局应用到ListView中,在布局中添加一些生动的元素,比如,图标。
一、为ListView每个item添加一个图标
先看效果:
首先定义一个entry.xml布局文件:
[html] view plaincopyprint?
1<span style="font-size:18px;">
2<?xml version="1.0" encoding="utf-8"?>
3<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4android:layout_width="fill_parent"
5 android:layout_height="60dp"
6 android:orientation="horizontal" >
7 <!-- 图标 -->
8 <ImageView
9 android:layout_width="48dp"
10android:layout_height="48dp"
11android:src="@drawable/preview"
12android:layout_gravity="center"
13android:layout_marginLeft="10dp"
14 />
15 <!-- 这里引用android自带的布局simple_list_item_1 -->
16<include
17 layout="@android:layout/simple_list_item_1"
18 />
19</LinearLayout>
20
21</span>
然后将此布局文件应用到ListView中去:
1[java] view plaincopyprint?
2<span style="font-size:18px;">
3public class ListViewDemo extends ListActivity {
4
5 private List<Map<String,String>> data = new ArrayList<Map<String,String>>();
6@Override
7 public void onCreate(Bundle savedInstanceState) {
8 super.onCreate(savedInstanceState);
9 setContentView(R.layout.main);
10 Map<String, String> map1 = new HashMap<String, String>();
11map1.put("姓名", "A君");
12 data.add(map1);
13 Map<String, String> map2 = new HashMap<String, String>();
14 map2.put("姓名", "B君");
15 data.add(map2);
16 setListAdapter(new SimpleAdapter(this,data,R.layout.entry,
17 new String[]{"姓名"},
18 new int[]{android.R.id.text1}
19 ));
20 }
21
22</span>
二、动态设置图标
以上例子,处理的非常简单,全部图标都一样,现在要想动态设置每个item中元素,需要继承Adapter子类,通过重写getView()函数,为每个item都设置不同的风格。
下面创建BaseAdapter的子类MyAdapter:
[java] view plaincopyprint?
1<span style="font-size:18px;">
2private class MyAdapter extends SimpleAdapter{
3 //icons存储图标的id
4 private int[] icons = null;
5
6private Context context;
7
8 public MyAdapter(Context context, List<? extends Map<String, ?>> data,
9 int resource, String[] from, int[] to,int[] icons) {
10super(context, data, resource, from, to);
11 // TODO Auto-generated constructor stub
12 this.icons = icons;//传入存储图标id的数组
13 this.context = context;
14 }
15
16 /* (non-Javadoc)
17* 重写getView函数,由于SimpleAdapter能处理TextView,仅仅是不能设置图标
18 */所以,只需要在调用父类的getView函数的基础上,设置上相应的图标即可
19 @Override
20 public View getView(int position, View convertView, ViewGroup parent) {
2 1 // TODO Auto-generated method stub
22 ViewHolder holder = null;
23 if(convertView == null){
24 holder = new ViewHolder();
25convertView = super.getView(position, convertView, parent);//调用父类的getView函数,设置TextView
26 holder.imageView = (ImageView)convertView.findViewById(R.id.myimageview);
27 convertView.setTag(holder);
28 }else {
29 holder = (ViewHolder)convertView.getTag();
30 }
31//从icons中随机选出一个,并设置作为item的图标
32Random random = new Random();
33 int index = random.nextInt(icons.length);
34 holder.imageView.setImageResource(icons[index]);
35
36 return convertView;
37 }
38
39private class ViewHolder{
40 ImageView imageView;
41 }
42 }
43
44</span>
MyAdapter继承自SimpleAdapter,重写getView()函数,使listView中每个item的图标都是随机选择的。对MyAdapter的应用如下:
[html] view plaincopyprint?
1<span style="font-size:18px;">
2//数组存储图标id
3private int[] myicons = { R.drawable.preview,R.drawable.preview1,R.drawable.preview2,R.drawable.preview3};
4
5</span>
[html] view plaincopyprint?
1<span style="font-size:18px;">
2setListAdapter(new </span><span style="font-size:18px;color:#FF0000;">MyAdapter</span><span style="font-size:18px;">(this, data, R.layout.entry, new String[]{"姓名"},
3 new int[]{android.R.id.text1},</span><span style="font-size:18px;color:#FF0000;"> myicons</span><span style="font-size:18px;">));//为ListView设置MyAdapter适配器
4
5</span>
上图: