前面有文章介紹了使用gridview實現表格的方法,本文就來說說如何用listview實現自適應的表格。gridview比listview更容易實現自適應的表格,但是gridview每個格單元的大小固定,而listview實現的表格可以自定義每個格單元的大小,但因此實現自適應表格也會復雜些(主要由于格單元大小不一)。此外,gridview實現的表格可以定位在具體某個格單元,而listview實現的表格則只能定位在表格行。因此還是那句老話:根據具體的使用環境而選擇gridview 或者 listview實現表格。
先來看看本文程序運行的效果圖,如下圖所示:
本文實現的listview表格,可以每個格單元大小不一,文本(textview)或圖片(imageview)做格單元的數據,不需要預先定義xml實現樣式(自適應的根本目標)。由于listview置于horizontalscrollview中,因此對于列比較多/列數據比較長的數據表也能很好地適應其寬度。
main.xml源碼如下:
1
2
3
4
5
6
7
8
9
10
|
<?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" > <horizontalscrollview android:id= "@+id/horizontalscrollview01" android:layout_height= "fill_parent" android:layout_width= "fill_parent" > <listview android:id= "@+id/listview01" android:layout_height= "wrap_content" android:layout_width= "wrap_content" ></listview> </horizontalscrollview> </linearlayout> |
主類testmylistview.java的源碼如下:
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
|
package com.testmylistview; import java.util.arraylist; import com.testmylistview.tableadapter.tablecell; import com.testmylistview.tableadapter.tablerow; import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.listview; import android.widget.linearlayout.layoutparams; import android.widget.toast; /** * @author hellogv */ public class testmylistview extends activity { /** called when the activity is first created. */ listview lv; @override public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.main); this .settitle( "listview自適應實現表格---hellogv" ); lv = (listview) this .findviewbyid(r.id.listview01); arraylist<tablerow> table = new arraylist<tablerow>(); tablecell[] titles = new tablecell[ 5 ]; // 每行5個單元 int width = this .getwindowmanager().getdefaultdisplay().getwidth()/titles.length; // 定義標題 for ( int i = 0 ; i < titles.length; i++) { titles[i] = new tablecell( "標題" + string.valueof(i), width + 8 * i, layoutparams.fill_parent, tablecell.string); } table.add( new tablerow(titles)); // 每行的數據 tablecell[] cells = new tablecell[ 5 ]; // 每行5個單元 for ( int i = 0 ; i < cells.length - 1 ; i++) { cells[i] = new tablecell( "no." + string.valueof(i), titles[i].width, layoutparams.fill_parent, tablecell.string); } cells[cells.length - 1 ] = new tablecell(r.drawable.icon, titles[cells.length - 1 ].width, layoutparams.wrap_content, tablecell.image); // 把表格的行添加到表格 for ( int i = 0 ; i < 12 ; i++) table.add( new tablerow(cells)); tableadapter tableadapter = new tableadapter( this , table); lv.setadapter(tableadapter); lv.setonitemclicklistener( new itemclickevent()); } class itemclickevent implements adapterview.onitemclicklistener { @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { toast.maketext(testmylistview. this , "選中第" +string.valueof(arg2)+ "行" , 500 ).show(); } } } |
listview自適應實現table的類tableadapter.java代碼如下:
此處需要注意:tablecell是格單元的類,tablerow是表格行的類,tablerowview是實現表格行的組件。實現步驟:tablecell --> tablerow(tablerowview)-->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
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
98
99
100
101
|
package com.testmylistview; import java.util.list; import android.content.context; import android.graphics.color; import android.view.gravity; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.linearlayout; import android.widget.textview; public class tableadapter extends baseadapter { private context context; private list<tablerow> table; public tableadapter(context context, list<tablerow> table) { this .context = context; this .table = table; } @override public int getcount() { return table.size(); } @override public long getitemid( int position) { return position; } public tablerow getitem( int position) { return table.get(position); } public view getview( int position, view convertview, viewgroup parent) { tablerow tablerow = table.get(position); return new tablerowview( this .context, tablerow); } /** * tablerowview 實現表格行的樣式 * @author hellogv */ class tablerowview extends linearlayout { public tablerowview(context context, tablerow tablerow) { super (context); this .setorientation(linearlayout.horizontal); for ( int i = 0 ; i < tablerow.getsize(); i++) { //逐個格單元添加到行 tablecell tablecell = tablerow.getcellvalue(i); linearlayout.layoutparams layoutparams = new linearlayout.layoutparams( tablecell.width, tablecell.height); //按照格單元指定的大小設置空間 layoutparams.setmargins( 0 , 0 , 1 , 1 ); //預留空隙制造邊框 if (tablecell.type == tablecell.string) { //如果格單元是文本內容 textview textcell = new textview(context); textcell.setlines( 1 ); textcell.setgravity(gravity.center); textcell.setbackgroundcolor(color.black); //背景黑色 textcell.settext(string.valueof(tablecell.value)); addview(textcell, layoutparams); } else if (tablecell.type == tablecell.image) { //如果格單元是圖像內容 imageview imgcell = new imageview(context); imgcell.setbackgroundcolor(color.black); //背景黑色 imgcell.setimageresource((integer) tablecell.value); addview(imgcell, layoutparams); } } this .setbackgroundcolor(color.white); //背景白色,利用空隙來實現邊框 } } /** * tablerow 實現表格的行 * @author hellogv */ static public class tablerow { private tablecell[] cell; public tablerow(tablecell[] cell) { this .cell = cell; } public int getsize() { return cell.length; } public tablecell getcellvalue( int index) { if (index >= cell.length) return null ; return cell[index]; } } /** * tablecell 實現表格的格單元 * @author hellogv */ static public class tablecell { static public final int string = 0 ; static public final int image = 1 ; public object value; public int width; public int height; private int type; public tablecell(object value, int width, int height, int type) { this .value = value; this .width = width; this .height = height; this .type = type; } } } |
希望本文所述實例能夠對大家進行android項目開發有所幫助。