Android提高第十五篇之ListView自适应实现表格

来源:互联网 发布:mac在线重装找不到磁盘 编辑:程序博客网 时间:2024/06/16 02:58

本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!

       上次介绍了使用GridView实现表格,这次就说说如何用ListView实现自适应的表格。GridView比ListView更容易实现自适应的表格,但是GridView每个格单元的大小固定,而ListView实现的表格可以自定义每个格单元的大小,但因此实现自适应表格也会复杂些(格单元大小不一)。另外,GridView实现的表格可以定位在具体某个格单元,而ListView实现的表格则只能定位在表格行。因此还是那句老话:根据具体的使用环境而选择GridView 或者 ListView实现表格。

先贴出本文程序运行的效果图:

本文实现的ListView表格,可以每个格单元大小不一,文本(TextView)或图片(ImageView)做格单元的数据,不需要预先定义XML实现样式(自适应的根本目标)。由于ListView置于HorizontalScrollView中,因此对于列比较多/列数据比较长的数据表也能很好地适应其宽度。

main.xml源码如下:

[xhtml] view plaincopyprint?
  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical"android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent"> 
  5.     <HorizontalScrollViewandroid:id="@+id/HorizontalScrollView01" 
  6.         android:layout_height="fill_parent"android:layout_width="fill_parent"> 
  7.         <ListViewandroid:id="@+id/ListView01"android:layout_height="wrap_content" 
  8.             android:layout_width="wrap_content"></ListView> 
  9.     </HorizontalScrollView> 
  10. </LinearLayout> 

主类testMyListView.java的源码如下:

[java] view plaincopyprint?
  1. package com.testMyListView; 
  2. import java.util.ArrayList; 
  3. import com.testMyListView.TableAdapter.TableCell; 
  4. import com.testMyListView.TableAdapter.TableRow; 
  5. import android.app.Activity; 
  6. import android.os.Bundle; 
  7. import android.view.View; 
  8. import android.widget.AdapterView; 
  9. import android.widget.ListView; 
  10. import android.widget.LinearLayout.LayoutParams; 
  11. import android.widget.Toast; 
  12. /**
  13. * @author hellogv
  14. */ 
  15. public class testMyListViewextends Activity { 
  16.     /** Called when the activity is first created. */ 
  17.     ListView lv; 
  18.     @Override 
  19.     public void onCreate(Bundle savedInstanceState) { 
  20.         super.onCreate(savedInstanceState); 
  21.         setContentView(R.layout.main); 
  22.         this.setTitle("ListView自适应实现表格---hellogv"); 
  23.         lv = (ListView) this.findViewById(R.id.ListView01); 
  24.         ArrayList<TableRow> table = new ArrayList<TableRow>(); 
  25.         TableCell[] titles = new TableCell[5];// 每行5个单元 
  26.         int width = this.getWindowManager().getDefaultDisplay().getWidth()/titles.length; 
  27.         // 定义标题 
  28.         for (int i =0; i < titles.length; i++) { 
  29.             titles[i] = new TableCell("标题" + String.valueOf(i),  
  30.                                     width + 8 * i, 
  31.                                     LayoutParams.FILL_PARENT,  
  32.                                     TableCell.STRING); 
  33.         } 
  34.         table.add(new TableRow(titles)); 
  35.         // 每行的数据 
  36.         TableCell[] cells = new TableCell[5];// 每行5个单元 
  37.         for (int i =0; i < cells.length - 1; i++) { 
  38.             cells[i] = new TableCell("No." + String.valueOf(i), 
  39.                                     titles[i].width,  
  40.                                     LayoutParams.FILL_PARENT,  
  41.                                     TableCell.STRING); 
  42.         } 
  43.         cells[cells.length - 1] =new TableCell(R.drawable.icon, 
  44.                                                 titles[cells.length - 1].width,  
  45.                                                 LayoutParams.WRAP_CONTENT, 
  46.                                                 TableCell.IMAGE); 
  47.         // 把表格的行添加到表格 
  48.         for (int i =0; i < 12; i++) 
  49.             table.add(new TableRow(cells)); 
  50.         TableAdapter tableAdapter = new TableAdapter(this, table); 
  51.         lv.setAdapter(tableAdapter); 
  52.         lv.setOnItemClickListener(new ItemClickEvent()); 
  53.     } 
  54.     class ItemClickEvent implements AdapterView.OnItemClickListener { 
  55.         @Override 
  56.         public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, 
  57.                 long arg3) { 
  58.             Toast.makeText(testMyListView.this,"选中第"+String.valueOf(arg2)+"行",500).show(); 
  59.         } 
  60.     } 

ListView自适应实现Table的类TableAdapter.java代码如下:

PS:TableCell是格单元的类,TableRow是表格行的类,TableRowView是实现表格行的组件。实现步骤:TableCell --> TableRow(TableRowView)-->ListView

[java] view plaincopyprint?
  1. package com.testMyListView; 
  2. import java.util.List; 
  3. import android.content.Context; 
  4. import android.graphics.Color; 
  5. import android.view.Gravity; 
  6. import android.view.View; 
  7. import android.view.ViewGroup; 
  8. import android.widget.BaseAdapter; 
  9. import android.widget.ImageView; 
  10. import android.widget.LinearLayout; 
  11. import android.widget.TextView; 
  12. public class TableAdapterextends BaseAdapter { 
  13.     private Context context; 
  14.     private List<TableRow> table; 
  15.     public TableAdapter(Context context, List<TableRow> table) { 
  16.         this.context = context; 
  17.         this.table = table; 
  18.     } 
  19.     @Override 
  20.     public int getCount() { 
  21.         return table.size(); 
  22.     } 
  23.     @Override 
  24.     public long getItemId(int position) { 
  25.         return position; 
  26.     } 
  27.     public TableRow getItem(int position) { 
  28.         return table.get(position); 
  29.     } 
  30.     public View getView(int position, View convertView, ViewGroup parent) { 
  31.         TableRow tableRow = table.get(position); 
  32.         return new TableRowView(this.context, tableRow); 
  33.     } 
  34.     /**
  35.      * TableRowView 实现表格行的样式
  36.      * @author hellogv
  37.      */ 
  38.     class TableRowView extends LinearLayout { 
  39.         public TableRowView(Context context, TableRow tableRow) { 
  40.             super(context); 
  41.              
  42.             this.setOrientation(LinearLayout.HORIZONTAL); 
  43.             for (int i =0; i < tableRow.getSize(); i++) {//逐个格单元添加到行 
  44.                 TableCell tableCell = tableRow.getCellValue(i); 
  45.                 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( 
  46.                         tableCell.width, tableCell.height);//按照格单元指定的大小设置空间 
  47.                 layoutParams.setMargins(0,0, 1,1);//预留空隙制造边框 
  48.                 if (tableCell.type == TableCell.STRING) {//如果格单元是文本内容 
  49.                     TextView textCell = new TextView(context); 
  50.                     textCell.setLines(1); 
  51.                     textCell.setGravity(Gravity.CENTER); 
  52.                     textCell.setBackgroundColor(Color.BLACK);//背景黑色 
  53.                     textCell.setText(String.valueOf(tableCell.value)); 
  54.                     addView(textCell, layoutParams); 
  55.                 } else if (tableCell.type == TableCell.IMAGE) {//如果格单元是图像内容 
  56.                     ImageView imgCell = new ImageView(context); 
  57.                     imgCell.setBackgroundColor(Color.BLACK);//背景黑色 
  58.                     imgCell.setImageResource((Integer) tableCell.value); 
  59.                     addView(imgCell, layoutParams); 
  60.                 } 
  61.             } 
  62.             this.setBackgroundColor(Color.WHITE);//背景白色,利用空隙来实现边框 
  63.         } 
  64.     } 
  65.     /**
  66.      * TableRow 实现表格的行
  67.      * @author hellogv
  68.      */ 
  69.     static publicclass TableRow { 
  70.         private TableCell[] cell; 
  71.         public TableRow(TableCell[] cell) { 
  72.             this.cell = cell; 
  73.         } 
  74.         public int getSize() { 
  75.             return cell.length; 
  76.         } 
  77.         public TableCell getCellValue(int index) { 
  78.             if (index >= cell.length) 
  79.                 return null
  80.             return cell[index]; 
  81.         } 
  82.     } 
  83.     /**
  84.      * TableCell 实现表格的格单元
  85.      * @author hellogv
  86.      */ 
  87.     static publicclass TableCell { 
  88.         static publicfinal int STRING =0
  89.         static publicfinal int IMAGE =1
  90.         public Object value; 
  91.         public int width; 
  92.         public int height; 
  93.         private int type; 
  94.         public TableCell(Object value,int width, int height,int type) { 
  95.             this.value = value; 
  96.             this.width = width; 
  97.             this.height = height; 
  98.             this.type = type; 
  99.         } 
  100.     }