前面有文章介绍了使用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项目开发有所帮助。