1. 程式人生 > >Android提高第十五篇之ListView自適應實現表格

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

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

本文來自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 plain copy 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>  
<?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的原始碼如下:

[java] view plain copy 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. /** 
  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<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.     }  
  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程式碼如下:

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

[java] view plain copy 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.     /** 
  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(0011);//預留空隙製造邊框  
  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 public class 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 public class TableCell {  
  88.         static public final int STRING = 0;  
  89.         static public final 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.     }  
  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;  } }}

           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述