上回再写了《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;
- }
- }
源码下载地址点击打开连接