zoukankan      html  css  js  c++  java
  • Android中使用ListView绘制自定义表格(2)

    上回再写了《Android中使用ListView绘制自定义表格》后,很多人留言代码不全和没有数据样例。但因为项目原因,没法把源码全部贴上来。近两天,抽空简化了一下,做了一个例子。

    效果图如

    一、功能:

         1、支持列合并

         2、考虑了界面刷新优化

         3、预留部分接口

         4、支持左右滚动

    1、枚举类:CellTypeEnum

    [java] view plaincopy
     
    1. package csdn.danielinbiti.custometableview.item;  
    2.   
    3. public enum CellTypeEnum {     
    4.      STRING   //字符  
    5.      ,DIGIT   //数字   
    6.      ,LABEL   //标签  
    7. }  

    该类定义了表格支持的样式,可以根据需要扩充,扩充了新类型,注意同时修改CustomeTableItem中新类型样式的创建方式


    2、核心代码CustomeTableItem,该类对应ListView的一行item。类支持列合并,没有实现行合并(行合并样式控制上会比较复杂些,如有需要自己改写吧)

    rowtype:该值主要表示表格中不同行的样式,如果合并的列都一样的行,则可以复用,不需要再创建了。

    [java] view plaincopy
     
    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. }  



    源码下载地址点击打开链接

  • 相关阅读:
    PostgreSQL的数据类型
    博客园背景页面动态特效
    css ie7中overflow:hidden失效问题及解决方法
    win10的安装、win10启动盘制作
    windows win7 win10 多系统启动菜单 多系统引导设置
    微博加关注按钮
    {转}一位北京差生9年的北京生活
    最全的CSS浏览器兼容问题
    网站开发命名详细规范
    <meta http-equiv = "X-UA-Compatible" cotent = "IE=edge,chrome=1"/>
  • 原文地址:https://www.cnblogs.com/dongweiq/p/3945383.html
Copyright © 2011-2022 走看看