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


1、功能:佈局
一、支持列合併優化
二、考慮了界面刷新優化ui
三、預留部分接口this
四、支持左右滾動atom
一、枚舉類:CellTypeEnumspa
- package csdn.danielinbiti.custometableview.item;
-
- public enum CellTypeEnum {
- STRING
- ,DIGIT
- ,LABEL
- }
該類定義了表格支持的樣式,能夠根據須要擴充,擴充了新類型,注意同時修改CustomeTableItem中新類型樣式的建立方式.net
二、核心代碼CustomeTableItem,該類對應ListView的一行item。類支持列合併,沒有實現行合併(行合併樣式控制上會比較複雜些,若有須要本身改寫吧)blog
rowtype:該值主要表示表格中不一樣行的樣式,若是合併的列都同樣的行,則能夠複用,不須要再建立了。
- package csdn.danielinbiti.custometableview.item;
-
- import java.util.ArrayList;
-
- import csdn.danielinbiti.custometableview.R;
-
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import android.widget.LinearLayout.LayoutParams;
-
- public class CustomeTableItem extends LinearLayout {
- private Context context = null;
- private boolean isRead = false;
- private ArrayList<View> viewList = new ArrayList();
- private int[] headWidthArr = null;
- private String rowType = "0";
-
- public CustomeTableItem(Context context) {
- super(context);
- }
- public CustomeTableItem(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public CustomeTableItem(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
-
- public void buildItem(Context context,String rowType,ArrayList<ItemCell> itemCells
- ,int[] headWidthArr,boolean isRead){
- this.setOrientation(LinearLayout.VERTICAL);
- this.context = context;
- this.headWidthArr =headWidthArr.clone();
- this.rowType = rowType;
-
- this.addCell(itemCells);
- }
- private void addCell(ArrayList<ItemCell> itemCells){
- this.removeAllViews();
- LinearLayout secondLayout = new LinearLayout(context);
- secondLayout.setOrientation(LinearLayout.HORIZONTAL);
- secondLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
- this.addView(secondLayout);
- int cellIndex = 0;
- for(int i=0;i<itemCells.size();i++){
- ItemCell itemCell = itemCells.get(i);
- int endIndex = cellIndex+itemCell.getCellSpan();
-
- int width = getCellWidth(cellIndex,endIndex);
- cellIndex = endIndex;
- if(itemCell.getCellType()==CellTypeEnum.STRING){
- EditText view= (EditText)getInputView();
- view.setText(itemCell.getCellValue());
- view.setWidth(width);
- this.setEditView(view);
- secondLayout.addView(view);
- viewList.add(view);
- }else if(itemCell.getCellType()==CellTypeEnum.DIGIT){
- EditText view= (EditText)getInputView();
- view.setText(itemCell.getCellValue());
- view.setWidth(width);
- this.setEditView(view);
- this.setOnKeyBorad(view);
- secondLayout.addView(view);
- viewList.add(view);
- }else if(itemCell.getCellType()==CellTypeEnum.LABEL){
- TextView view = (TextView)getLabelView();
- view.setText(itemCell.getCellValue());
- view.setWidth(width);
- secondLayout.addView(view);
- viewList.add(view);
- }
- if(i!=itemCells.size()-1){
- LinearLayout v_line = (LinearLayout)getVerticalLine();
- v_line.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
- secondLayout.addView(v_line);
- }
- }
- }
- public void refreshData(ArrayList<ItemCell> itemCells){
- for(int i=0;i<itemCells.size();i++){
- ItemCell itemCell = itemCells.get(i);
- if(itemCell.getCellType()==CellTypeEnum.LABEL){
- TextView view = (TextView)viewList.get(i);
- view.setText(itemCell.getCellValue());
- }else if(itemCell.getCellType()==CellTypeEnum.DIGIT){
- EditText view= (EditText)viewList.get(i);
- view.setText(itemCell.getCellValue());
- this.setEditView(view);
- this.setOnKeyBorad(view);
- }else if(itemCell.getCellType()==CellTypeEnum.STRING){
- EditText view= (EditText)viewList.get(i);
- view.setText(itemCell.getCellValue());
- this.setEditView(view);
- }
- }
- }
- private View getVerticalLine(){
- return LayoutInflater.from(context).inflate(R.layout.atom_line_v_view, null);
- }
- private int getCellWidth(int cellStart,int cellEnd){
- int width = 0;
- for(int i=cellStart;i<cellEnd;i++){
- width = this.headWidthArr[i] + width;
- }
- return width;
- }
- private View getLabelView(){
- return (View)LayoutInflater.from(context).inflate(R.layout.atom_text_view, null);
- }
- private View getInputView(){
- return (View)LayoutInflater.from(context).inflate(R.layout.atom_edttxt_view, null);
- }
- private void setEditView(EditText edtText1){
- if(this.isRead){
- edtText1.setEnabled(false);
- }else{
-
- }
- }
- private void setOnKeyBorad(EditText edtText1){
-
- if(!this.isRead){
-
- }
- }
- public String getRowType() {
- return rowType;
- }
- }
源碼下載地址點擊打開連接