一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - Android提高之ListView實現自適應表格的方法

Android提高之ListView實現自適應表格的方法

2021-03-07 14:45Android開發網 Android

這篇文章主要介紹了Android采用ListView實現自適應表格的方法,比較實用的功能,需要的朋友可以參考下

前面有文章介紹了使用gridview實現表格的方法,本文就來說說如何用listview實現自適應的表格。gridview比listview更容易實現自適應的表格,但是gridview每個格單元的大小固定,而listview實現的表格可以自定義每個格單元的大小,但因此實現自適應表格也會復雜些(主要由于格單元大小不一)。此外,gridview實現的表格可以定位在具體某個格單元,而listview實現的表格則只能定位在表格行。因此還是那句老話:根據具體的使用環境而選擇gridview 或者 listview實現表格。

先來看看本文程序運行的效果圖,如下圖所示:

Android提高之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項目開發有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 538亚洲欧美国产日韩在线精品 | 热剧库| 1024在线视频精品免费 | 国产爽视频 | 国产成+人+综合+亚洲欧美丁香花 | 久久中文电影 | 国产日韩一区二区三区 | 久久精品亚洲精品国产欧美 | 超级毛片 | 久久久GOGO无码啪啪艺术 | 国产精品麻豆 | 男人的天堂va | 国产午夜久久精品 | 日韩久久精品 | 青苹果乐园影院免费观看完整版 | 女同志freelesvoices | 娇妻被健身教练挺进小说阅读 | 亚洲成人免费看 | 纲手被强喷水羞羞漫画 | 97自拍视频在线观看 | 国产成人精品免费午夜 | 欧美日韩亚洲一区二区三区在线观看 | 草莓秋葵菠萝蜜绿巨人污 | 日本视频一区在线观看免费 | 图片一区 | 91麻豆精品国产自产在线观看 | 91美女在线 | 久久精品一区二区三区资源网 | 免看一级一片一在线看 | 91制片在线观看 | 艹b小说| 精品91一区二区三区 | aaaa黄色片 | 婷婷日韩 | 星空无限传媒xk8046 | 欧美精品一区二区三区免费播放 | 欧美一级在线播放 | 手机国产乱子伦精品视频 | 人人看人人射 | 欧美成人香蕉在线观看 | 久久偷拍人 |