一、直接使用listview组件创建
1.直接在xml中创建listview用entries属性附上一个数组资源
其中divider属性是设置分割线可以使用颜色和drawable资源分割
1
2
3
4
5
6
7
8
9
10
|
<listview android:id= "@+id/listview1" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:divider= "#33000000" android:dividerheight= "0.2dp" android:footerdividersenabled= "true" android:headerdividersenabled= "true" android:entries= "@arrary/ctype" > </listview> |
在values下定义一个数组资源文件arrays.xml
1
2
3
4
5
|
<resources> <item >情景模式 1 </item> <item >情景模式 2 </item> <item >情景模式 3 </item> </resources> |
2.创建arrayadapter指定要显示的列表项
在oncreat方法中关联适配器
simple_list_item_1:列表项为普通文本
simple_list_item_2:列表项为普通文本字体略大
simple_list_item_checked:列表项为一个已选中的
simple_list_item_multiple_choice:带有复选框的列表项
simple_list_item_single_choice:带有单选按钮的列表项
1
2
3
|
listview = (listview) findviewbyid(r.id.listview1); arrayadapter<charsequence> adapter = arrayadapter.createfromresource( this ,r.array.ctype, android.r.layout.simple_list_item_checked); listview.setadapter(adapter); |
二、让activity继承listactivity实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class activitymain extends listactivity { @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); string[] ctype ={ "1" , "2" , "3" }; arrayadapter<string> adapter = new arrayadapter<string>( this ,android.r.layout.simple_list_item_1,ctype); setlistadapter(adapter); } @override protected void onlistitemclick(listview l, view v, int position, long id) { super .onlistitemclick(l, v, position, id); } } |
三、simpleadapter和baseaapter的使用(重点)
simpleadapter的使用
在main.xml中添加listview组件
编写用于布局列表项内容的布局文件items.xml
创建一个简单适配器与listview关联
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?xml version= "1.0" encoding= "utf-8" ?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "horizontal" android:layout_width= "match_parent" android:layout_height= "match_parent" > <imageview android:id= "@+id/image" android:paddingright= "10px" android:paddingtop= "20px" android:paddingbottom= "20px" android:adjustviewbounds= "true" android:maxwidth= "72px" android:maxheight= "72px" android:layout_height= "wrap_content" android:layout_width= "wrap_content" /> <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:padding= "10px" android:layout_gravity= "center" android:id= "@+id/title" /> </linearlayout> |
- public class mainactivity extends activity {
- @override
- public void oncreate(bundle savedinstancestate) {
- super.oncreate(savedinstancestate);
- setcontentview(r.layout.main);
- listview listview = (listview) findviewbyid(r.id.listview1); // 获取列表视图
- int[] imageid = new int[] { r.drawable.img01, r.drawable.img02,
- r.drawable.img03, r.drawable.img04, r.drawable.img05,
- r.drawable.img06, r.drawable.img07, r.drawable.img08 }; // 定义并初始化保存图片id的数组
- string[] title = new string[] { "保密设置", "安全", "系统设置", "上网", "我的文档",
- "gps导航", "我的音乐", "e-mail" }; // 定义并初始化保存列表项文字的数组
- list<map<string, object>> listitems = new arraylist<map<string, object>>(); // 创建一个list集合
- // 通过for循环将图片id和列表项文字放到map中,并添加到list集合中
- for (int i = 0; i < imageid.length; i++) {
- map<string, object> map = new hashmap<string, object>(); // 实例化map对象
- map.put("image", imageid[i]);
- map.put("title", title[i]);
- listitems.add(map); // 将map对象添加到list集合中
- }
- simpleadapter adapter = new simpleadapter(this, listitems,
- r.layout.items, new string[] { "title", "image" }, new int[] {
- r.id.title, r.id.image }); // 创建simpleadapter
- listview.setadapter(adapter); // 将适配器与listview关联
- }
- }
三、baseadapter的使用及listview的优化
使用方法同上主要是baseadapter的使用更加灵活也更加强大,可以向其中添加其他控件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<?xml version= "1.0" encoding= "utf-8" ?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <imageview android:id= "@+id/imageview1" android:layout_width= "50dip" android:layout_height= "50dip" android:layout_marginleft= "5dip" android:layout_margintop= "5dip" android:layout_marginbottom= "5dip" android:src= "@android:drawable/ic_lock_power_off" /> <textview android:id= "@+id/textview1" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_above= "@+id/textview2" android:layout_torightof= "@+id/imageview1" android:text= "name" android:textcolor= "#000000" android:textsize= "20sp" /> <textview android:id= "@+id/textview2" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignbottom= "@+id/imageview1" android:layout_torightof= "@+id/imageview1" android:text= "number" android:textcolor= "#000000" android:textsize= "20sp" /> </relativelayout> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
public class mainactivity extends activity { private listview lv; private list<myinfo> infos; private random random; @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_main); lv = (listview) findviewbyid(r.id.listview1); infos = new arraylist<myinfo>(); //通过for循环将name和number添加到list集合中 for ( int i = 0 ; i < 100 ; i++) { myinfo userinfo = new myinfo(); userinfo.setname( "name" +i); userinfo.setnumber( "number" +i); infos.add(userinfo); system.out.println(userinfo.tostring()); } lv.setadapter( new myadapter()); } private class myadapter extends baseadapter{ @override public int getcount() { return infos.size(); //返回listview的长度 } @override public view getview( int position, view convertview, viewgroup parent) { myinfo userinfo = infos.get(position); view view; viewholder holder; //减少内存中view对象创建的次数 if (convertview != null && convertview instanceof relativelayout) { view = convertview; //复用已经回收掉的view对象 holder = (viewholder) view.gettag(); //得到他们的引用 } else { view = view.inflate(getapplicationcontext(), r.layout.item_layout, null ); //把布局文件转化成view对象 holder = new viewholder(); //把id存到存到holder对象中 //注意是在view下findviewbyid holder.iv = (imageview) view.findviewbyid(r.id.imageview1); holder.tv1 = (textview) view.findviewbyid(r.id.textview1); holder.tv2 = (textview) view.findviewbyid(r.id.textview2); view.settag(holder); //对象创建出来时找到他们的引用存到holder中 } holder.tv1.settext(infos.get(position).getname()); holder.tv2.settext(infos.get(position).getnumber()); return view; } @override public object getitem( int position) { return null ; } @override public long getitemid( int position) { return 0 ; } } /* * view对象的容器记录view对象的内存地址相当于一个记事本 */ static class viewholder{ textview tv1; textview tv2; imageview iv; } } public class myinfo { private string name; private string number; //也可以添加bitmap @override public string tostring() { return "myinfo [name=" + name + ", number=" + number + "]" ; } public string getname() { return name; } public void setname(string name) { this .name = name; } public string getnumber() { return number; } public void setnumber(string number) { this .number = number; } } |
注意:(很多人不注意下面的这条语句是在view下findviewbyid,少了view会造成空指针异常)
1
|
holder.iv = (imageview) view.findviewbyid(r.id.imageview1); |
优化的原理就是动态循环复用已经回收掉的listview对象(回收后为convertview),保持一个界面下的listview的条目数的动态平衡。(具体内容参考代码注释即可)