天天看點

ListView自适應實作表格

http://blog.csdn.net/hellogv/archive/2010/12/14/6075014.aspx

上次介紹了使用GridView實作表格,這次就說說如何用ListView實作自适應的表格。GridView比ListView更容易實作自适應的表格,但是GridView每個格單元的大小固定,而ListView實作的表格可以自定義每個格單元的大小,但是以實作自适應表格也會複雜些(格單元大小不一)。另外,GridView實作的表格可以定位在具體某個格單元,而ListView實作的表格則隻能定位在表格行。是以還是那句老話:根據具體的使用環境而選擇GridView 或者 ListView實作表格。

先貼出本文程式運作的效果圖:

ListView自适應實作表格

本文實作的ListView表格,可以每個格單元大小不一,文本(TextView)或圖檔(ImageView)做格單元的資料,不需要預先定義XML實作樣式(自适應的根本目标)。由于ListView置于HorizontalScrollView中,是以對于列比較多/列資料比較長的資料表也能很好地适應其寬度。

main.xml源碼如下:

view plain copy to clipboard print ?

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <HorizontalScrollView android:id="@+id/HorizontalScrollView01"  
  6.         android:layout_height="fill_parent" android:layout_width="fill_parent">  
  7.         <ListView android:id="@+id/ListView01" android:layout_height="wrap_content"  
  8.             android:layout_width="wrap_content"></ListView>  
  9.     </HorizontalScrollView>  
  10. </LinearLayout>  

主類testMyListView.java的源碼如下:

view plain copy to clipboard print ?

  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. public class testMyListView extends Activity {  
  13.     ListView lv;  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         this.setTitle("ListView自适應實作表格---hellogv");  
  19.         lv = (ListView) this.findViewById(R.id.ListView01);  
  20.         ArrayList<TableRow> table = new ArrayList<TableRow>();  
  21.         TableCell[] titles = new TableCell[5];// 每行5個單元  
  22.         int width = this.getWindowManager().getDefaultDisplay().getWidth()/titles.length;  
  23.         // 定義标題  
  24.         for (int i = 0; i < titles.length; i++) {  
  25.             titles[i] = new TableCell("标題" + String.valueOf(i),   
  26.                                     width + 8 * i,  
  27.                                     LayoutParams.FILL_PARENT,   
  28.                                     TableCell.STRING);  
  29.         }  
  30.         table.add(new TableRow(titles));  
  31.         // 每行的資料  
  32.         TableCell[] cells = new TableCell[5];// 每行5個單元  
  33.         for (int i = 0; i < cells.length - 1; i++) {  
  34.             cells[i] = new TableCell("No." + String.valueOf(i),  
  35.                                     titles[i].width,   
  36.                                     LayoutParams.FILL_PARENT,   
  37.                                     TableCell.STRING);  
  38.         }  
  39.         cells[cells.length - 1] = new TableCell(R.drawable.icon,  
  40.                                                 titles[cells.length - 1].width,   
  41.                                                 LayoutParams.WRAP_CONTENT,  
  42.                                                 TableCell.IMAGE);  
  43.         // 把表格的行添加到表格  
  44.         for (int i = 0; i < 12; i++)  
  45.             table.add(new TableRow(cells));  
  46.         TableAdapter tableAdapter = new TableAdapter(this, table);  
  47.         lv.setAdapter(tableAdapter);  
  48.         lv.setOnItemClickListener(new ItemClickEvent());  
  49.     }  
  50.     class ItemClickEvent implements AdapterView.OnItemClickListener {  
  51.         @Override  
  52.         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  53.                 long arg3) {  
  54.             Toast.makeText(testMyListView.this, "選中第"+String.valueOf(arg2)+"行", 500).show();  
  55.         }  
  56.     }  
  57. }  

ListView自适應實作Table的類TableAdapter.java代碼如下:

PS:TableCell是格單元的類,TableRow是表格行的類,TableRowView是實作表格行的元件。實作步驟:TableCell --> TableRow(TableRowView)-->ListView

view plain copy to clipboard print ?

  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 TableAdapter extends 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.     class TableRowView extends LinearLayout {  
  35.         public TableRowView(Context context, TableRow tableRow) {  
  36.             super(context);  
  37.             this.setOrientation(LinearLayout.HORIZONTAL);  
  38.             for (int i = 0; i < tableRow.getSize(); i++) {//逐個格單元添加到行  
  39.                 TableCell tableCell = tableRow.getCellValue(i);  
  40.                 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(  
  41.                         tableCell.width, tableCell.height);//按照格單元指定的大小設定空間  
  42.                 layoutParams.setMargins(0, 0, 1, 1);//預留白隙制造邊框  
  43.                 if (tableCell.type == TableCell.STRING) {//如果格單元是文本内容  
  44.                     TextView textCell = new TextView(context);  
  45.                     textCell.setLines(1);  
  46.                     textCell.setGravity(Gravity.CENTER);  
  47.                     textCell.setBackgroundColor(Color.BLACK);//背景黑色  
  48.                     textCell.setText(String.valueOf(tableCell.value));  
  49.                     addView(textCell, layoutParams);  
  50.                 } else if (tableCell.type == TableCell.IMAGE) {//如果格單元是圖像内容  
  51.                     ImageView imgCell = new ImageView(context);  
  52.                     imgCell.setBackgroundColor(Color.BLACK);//背景黑色  
  53.                     imgCell.setImageResource((Integer) tableCell.value);  
  54.                     addView(imgCell, layoutParams);  
  55.                 }  
  56.             }  
  57.             this.setBackgroundColor(Color.WHITE);//背景白色,利用空隙來實作邊框  
  58.         }  
  59.     }  
  60.     static public class TableRow {  
  61.         private TableCell[] cell;  
  62.         public TableRow(TableCell[] cell) {  
  63.             this.cell = cell;  
  64.         }  
  65.         public int getSize() {  
  66.             return cell.length;  
  67.         }  
  68.         public TableCell getCellValue(int index) {  
  69.             if (index >= cell.length)  
  70.                 return null;  
  71.             return cell[index];  
  72.         }  
  73.     }  
  74.     static public class TableCell {  
  75.         static public final int STRING = 0;  
  76.         static public final int IMAGE = 1;  
  77.         public Object value;  
  78.         public int width;  
  79.         public int height;  
  80.         private int type;  
  81.         public TableCell(Object value, int width, int height, int type) {  
  82.             this.value = value;  
  83.             this.width = width;  
  84.             this.height = height;  
  85.             this.type = type;  
  86.         }  
  87.     }  
  88. }