Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android提高21篇之十五:ListView自適應實現表格

Android提高21篇之十五:ListView自適應實現表格

編輯:關於android開發

       說說如何用ListView實現自適應的表格。GridView比ListView更容易實現自適應的表格,但是GridView每個格單元的大小固定,而ListView實現的表格可以自定義每個格單元的大小,但因此實現自適應表格也會復雜些(格單元大小不一)。另外,GridView實現的表格可以定位在具體某個格單元,而ListView實現的表格則只能定位在表格行。因此還是那句老話:根據具體的使用環境而選擇GridView 或者 ListView實現表格。先貼出本文程序運行的效果圖:

Android提高21篇之十五:ListView自適應實現表格

       本文實現的ListView表格,可以每個格單元大小不一,文本(TextView)或圖片(ImageView)做格單元的數據,不需要預先定義XML實現樣式(自適應的根本目標)。由於ListView置於HorizontalScrollView中,因此對於列比較多/列數據比較長的數據表也能很好地適應其寬度。main.xml源碼如下:

XML/HTML代碼
  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的源碼如下:

Java代碼
  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 testMyListView extends 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[table]  
  25.  table = new ArrayList[table]  
  26. ();  
  27.                 TableCell[] titles = new TableCell[5];// 每行5個單元  
  28.                 int width = this.getWindowManager().getDefaultDisplay().getWidth()/titles.length;  
  29.                 // 定義標題  
  30.                 for (int i = 0; i < titles.length; i++) {  
  31.                         titles[i] = new TableCell("標題" + String.valueOf(i),   
  32.                                                                         width + 8 * i,  
  33.                                                                         LayoutParams.FILL_PARENT,   
  34.                                                                         TableCell.STRING);  
  35.                 }  
  36.                 table.add(new TableRow(titles));  
  37.                 // 每行的數據  
  38.                 TableCell[] cells = new TableCell[5];// 每行5個單元  
  39.                 for (int i = 0; i < cells.length - 1; i++) {  
  40.                         cells[i] = new TableCell("No." + String.valueOf(i),  
  41.                                                                         titles[i].width,   
  42.                                                                         LayoutParams.FILL_PARENT,   
  43.                                                                         TableCell.STRING);  
  44.                 }  
  45.                 cells[cells.length - 1] = new TableCell(R.drawable.icon,  
  46.                                                                                                 titles[cells.length - 1].width,   
  47.                                                                                                 LayoutParams.WRAP_CONTENT,  
  48.                                                                                                 TableCell.IMAGE);  
  49.                 // 把表格的行添加到表格  
  50.                 for (int i = 0; i < 12; i++)  
  51.                         table.add(new TableRow(cells));  
  52.                 TableAdapter tableAdapter = new TableAdapter(this, table);  
  53.                 lv.setAdapter(tableAdapter);  
  54.                 lv.setOnItemClickListener(new ItemClickEvent());  
  55.         }  
  56.         class ItemClickEvent implements AdapterView.OnItemClickListener {  
  57.                 @Override  
  58.                 public void onItemClick(AdapterView arg0, View arg1, int arg2,  
  59.                                 long arg3) {  
  60.                         Toast.makeText(testMyListView.this, "選中第"+String.valueOf(arg2)+"行", 500).show();  
  61.                 }  
  62.         }  
  63. }  

       ListView自適應實現Table的類TableAdapter.java代碼如下:PS:TableCell是格單元的類,TableRow是表格行的類,TableRowView是實現表格行的組件。實現步驟:TableCell --> TableRow(TableRowView)-->ListView

Java代碼
  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[table]  
  15.  table;  
  16.         public TableAdapter(Context context, List[table]  
  17.  table) {  
  18.                 this.context = context;  
  19.                 this.table = table;  
  20.         }  
  21.         @Override  
  22.         public int getCount() {  
  23.                 return table.size();  
  24.         }  
  25.         @Override  
  26.         public long getItemId(int position) {  
  27.                 return position;  
  28.         }  
  29.         public TableRow getItem(int position) {  
  30.                 return table.get(position);  
  31.         }  
  32.         public View getView(int position, View convertView, ViewGroup parent) {  
  33.                 TableRow tableRow = table.get(position);  
  34.                 return new TableRowView(this.context, tableRow);  
  35.         }  
  36.         /** 
  37.          * TableRowView 實現表格行的樣式 
  38.          * @author hellogv 
  39.          */  
  40.         class TableRowView extends LinearLayout {  
  41.                 public TableRowView(Context context, TableRow tableRow) {  
  42.                         super(context);  
  43.                           
  44.                         this.setOrientation(LinearLayout.HORIZONTAL);  
  45.                         for (int i = 0; i < tableRow.getSize(); i++) {//逐個格單元添加到行  
  46.                                 TableCell tableCell = tableRow.getCellValue(i);  
  47.                                 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(  
  48.                                                 tableCell.width, tableCell.height);//按照格單元指定的大小設置空間  
  49.                                 layoutParams.setMargins(0, 0, 1, 1);//預留空隙制造邊框  
  50.                                 if (tableCell.type == TableCell.STRING) {//如果格單元是文本內容  
  51.                                         TextView textCell = new TextView(context);  
  52.                                         textCell.setLines(1);  
  53.                                         textCell.setGravity(Gravity.CENTER);  
  54.                                         textCell.setBackgroundColor(Color.BLACK);//背景黑色  
  55.                                         textCell.setText(String.valueOf(tableCell.value));  
  56.                                         addView(textCell, layoutParams);  
  57.                                 } else if (tableCell.type == TableCell.IMAGE) {//如果格單元是圖像內容  
  58.                                         ImageView imgCell = new ImageView(context);  
  59.                                         imgCell.setBackgroundColor(Color.BLACK);//背景黑色  
  60.                                         imgCell.setImageResource((Integer) tableCell.value);  
  61.                                         addView(imgCell, layoutParams);  
  62.                                 }  
  63.                         }  
  64.                         this.setBackgroundColor(Color.WHITE);//背景白色,利用空隙來實現邊框  
  65.                 }  
  66.         }  
  67.         /** 
  68.          * TableRow 實現表格的行 
  69.          * @author hellogv 
  70.          */  
  71.         static public class TableRow {  
  72.                 private TableCell[] cell;  
  73.                 public TableRow(TableCell[] cell) {  
  74.                         this.cell = cell;  
  75.                 }  
  76.                 public int getSize() {  
  77.                         return cell.length;  
  78.                 }  
  79.                 public TableCell getCellValue(int index) {  
  80.                         if (index >= cell.length)  
  81.                                 return null;  
  82.                         return cell[index];  
  83.                 }  
  84.         }  
  85.         /** 
  86.          * TableCell 實現表格的格單元 
  87.          * @author hellogv 
  88.          */  
  89.         static public class TableCell {  
  90.                 static public final int STRING = 0;  
  91.                 static public final int IMAGE = 1;  
  92.                 public Object value;  
  93.                 public int width;  
  94.                 public int height;  
  95.                 private int type;  
  96.                 public TableCell(Object value, int width, int height, int type) {  
  97.                         this.value = value;  
  98.                         this.width = width;  
  99.                         this.height = height;  
  100.                         this.type = type;  
  101.                 }  
  102.         }  
  103. }  
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved