Android中使用ListView繪製自定義表格(2)

上回再寫了《Android中使用ListView繪製自定義表格》後,不少人留言代碼不全和沒有數據樣例。但由於項目緣由,無法把源碼所有貼上來。近兩天,抽空簡化了一下,作了一個例子。java

效果圖如android

1、功能:佈局

     一、支持列合併優化

     二、考慮了界面刷新優化ui

     三、預留部分接口this

     四、支持左右滾動atom

一、枚舉類:CellTypeEnumspa

 

[java]  view plain copy
 
  1. package csdn.danielinbiti.custometableview.item;  
  2.   
  3. public enum CellTypeEnum {     
  4.      STRING   //字符  
  5.      ,DIGIT   //數字   
  6.      ,LABEL   //標籤  
  7. }  

該類定義了表格支持的樣式,能夠根據須要擴充,擴充了新類型,注意同時修改CustomeTableItem中新類型樣式的建立方式.net

 


二、核心代碼CustomeTableItem,該類對應ListView的一行item。類支持列合併,沒有實現行合併(行合併樣式控制上會比較複雜些,若有須要本身改寫吧)blog

rowtype:該值主要表示表格中不一樣行的樣式,若是合併的列都同樣的行,則能夠複用,不須要再建立了。

 

[java]  view plain copy
 
  1. package csdn.danielinbiti.custometableview.item;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import csdn.danielinbiti.custometableview.R;  
  6.   
  7. import android.content.Context;  
  8. import android.util.AttributeSet;  
  9. import android.view.LayoutInflater;  
  10. import android.view.View;  
  11. import android.widget.EditText;  
  12. import android.widget.LinearLayout;  
  13. import android.widget.TextView;  
  14. import android.widget.LinearLayout.LayoutParams;  
  15.   
  16. public class CustomeTableItem extends LinearLayout {  
  17.     private Context context = null;  
  18.     private boolean isRead = false;//是否只讀  
  19.     private ArrayList<View> viewList = new ArrayList();//行的表格列表  
  20.     private int[] headWidthArr = null;//表頭的列寬設置  
  21.     private String rowType = "0";//行的樣式id  
  22.       
  23.     public CustomeTableItem(Context context) {  
  24.         super(context);  
  25.     }  
  26.     public CustomeTableItem(Context context, AttributeSet attrs) {  
  27.         super(context, attrs);  
  28.     }  
  29.     public CustomeTableItem(Context context, AttributeSet attrs, int defStyle) {  
  30.         super(context, attrs, defStyle);  
  31.     }  
  32.     /* 
  33.      * rowType:行的樣式,字符任意,相一樣式的行不須要再建立了 
  34.      * itemCells:單元格信息 
  35.      * headWidthArr:每列寬度 
  36.      * isRead:是否只讀,若是是隻讀,則全部的輸入都不生效 
  37.      */  
  38.     public void buildItem(Context context,String rowType,ArrayList<ItemCell> itemCells  
  39.             ,int[] headWidthArr,boolean isRead){  
  40.         this.setOrientation(LinearLayout.VERTICAL);//第一層佈局垂直  
  41.         this.context = context;  
  42.         this.headWidthArr =headWidthArr.clone();  
  43.         this.rowType = rowType;  
  44.           
  45.         this.addCell(itemCells);  
  46.     }  
  47.     private void addCell(ArrayList<ItemCell> itemCells){  
  48.         this.removeAllViews();  
  49.         LinearLayout secondLayout = new LinearLayout(context);  
  50.         secondLayout.setOrientation(LinearLayout.HORIZONTAL);  
  51.         secondLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));  
  52.         this.addView(secondLayout);  
  53.         int cellIndex = 0;  
  54.         for(int i=0;i<itemCells.size();i++){  
  55.             ItemCell itemCell = itemCells.get(i);  
  56.             int endIndex = cellIndex+itemCell.getCellSpan();//所佔行數  
  57.   
  58.             int width = getCellWidth(cellIndex,endIndex);//行寬度  
  59.             cellIndex = endIndex;  
  60.             if(itemCell.getCellType()==CellTypeEnum.STRING){  
  61.                 EditText view= (EditText)getInputView();  
  62.                 view.setText(itemCell.getCellValue());  
  63.                 view.setWidth(width);  
  64.                 this.setEditView(view);  
  65.                 secondLayout.addView(view);  
  66.                 viewList.add(view);  
  67.             }else if(itemCell.getCellType()==CellTypeEnum.DIGIT){  
  68.                 EditText view= (EditText)getInputView();  
  69.                 view.setText(itemCell.getCellValue());  
  70.                 view.setWidth(width);  
  71.                 this.setEditView(view);  
  72.                 this.setOnKeyBorad(view);  
  73.                 secondLayout.addView(view);  
  74.                 viewList.add(view);  
  75.             }else if(itemCell.getCellType()==CellTypeEnum.LABEL){  
  76.                 TextView view = (TextView)getLabelView();  
  77.                 view.setText(itemCell.getCellValue());  
  78.                 view.setWidth(width);  
  79.                 secondLayout.addView(view);  
  80.                 viewList.add(view);  
  81.             }  
  82.             if(i!=itemCells.size()-1){//插入豎線  
  83.                 LinearLayout v_line = (LinearLayout)getVerticalLine();  
  84.                 v_line.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));  
  85.                 secondLayout.addView(v_line);  
  86.             }  
  87.         }  
  88.     }  
  89.     public void refreshData(ArrayList<ItemCell> itemCells){  
  90.         for(int i=0;i<itemCells.size();i++){  
  91.             ItemCell itemCell = itemCells.get(i);  
  92.             if(itemCell.getCellType()==CellTypeEnum.LABEL){  
  93.                 TextView view = (TextView)viewList.get(i);  
  94.                 view.setText(itemCell.getCellValue());  
  95.             }else if(itemCell.getCellType()==CellTypeEnum.DIGIT){  
  96.                 EditText view= (EditText)viewList.get(i);  
  97.                 view.setText(itemCell.getCellValue());  
  98.                 this.setEditView(view);  
  99.                 this.setOnKeyBorad(view);  
  100.             }else if(itemCell.getCellType()==CellTypeEnum.STRING){  
  101.                 EditText view= (EditText)viewList.get(i);  
  102.                 view.setText(itemCell.getCellValue());  
  103.                 this.setEditView(view);  
  104.             }  
  105.         }  
  106.     }  
  107.     private View getVerticalLine(){  
  108.         return LayoutInflater.from(context).inflate(R.layout.atom_line_v_view, null);  
  109.     }  
  110.     private int getCellWidth(int cellStart,int cellEnd){  
  111.         int width = 0;  
  112.         for(int i=cellStart;i<cellEnd;i++){  
  113.             width = this.headWidthArr[i] + width;  
  114.         }  
  115.         return width;  
  116.     }  
  117.     private View getLabelView(){  
  118.         return (View)LayoutInflater.from(context).inflate(R.layout.atom_text_view, null);  
  119.     }  
  120.     private View getInputView(){  
  121.         return (View)LayoutInflater.from(context).inflate(R.layout.atom_edttxt_view, null);  
  122.     }  
  123.     private void setEditView(EditText edtText1){  
  124.         if(this.isRead){  
  125.             edtText1.setEnabled(false);  
  126.         }else{  
  127.               
  128.         }  
  129.     }  
  130.     private void setOnKeyBorad(EditText edtText1){  
  131.         //數字鍵盤  
  132.         if(!this.isRead){//非只讀  
  133.               
  134.         }  
  135.     }  
  136.     public String getRowType() {  
  137.         return rowType;  
  138.     }  
  139. }  



 

源碼下載地址點擊打開連接

相關文章
相關標籤/搜索