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

效果图如

一、功能:

1、支持列合并

2、考虑了界面刷新优化

3、预留部分接口

4、支持左右滚动

1、枚举类:CellTypeEnum

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

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

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

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

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

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

Android中使用ListView绘制自定义表格(2)的更多相关文章

  1. Android中使用ListView实现自适应表格

    GridView比ListView更容易实现自适应的表格,但是GridView每个格单元的大小固定,而ListView实现的表格可以自定义每个格单元的大小,但因此实现自适应表格也会复杂些(格单元大小不 ...

  2. Android中View的绘制过程 onMeasure方法简述 附有自定义View例子

    Android中View的绘制过程 onMeasure方法简述 附有自定义View例子 Android中View的绘制过程 当Activity获得焦点时,它将被要求绘制自己的布局,Android fr ...

  3. 【转】Android中View的绘制过程 onMeasure方法简述 附有自定义View例子

    Android中View的绘制过程 当Activity获得焦点时,它将被要求绘制自己的布局,Android framework将会处理绘制过程,Activity只需提供它的布局的根节点. 绘制过程从布 ...

  4. 【转】整理一下Android中的ListView

    原文网址:http://sunbofu.blog.51cto.com/6431507/1280441 Android中的listview目测是一个使用频率很高的组件,所以今天来总结一下listview ...

  5. Android中的ListView属性使用总结

    Android中使用ListView控件比较常见,如果能知道常用的一些属性使用,肯定会少很多坑. 1.ListView是常用的显示控件,默认背景是和系统窗口一样的透明色,如果给ListView加上背景 ...

  6. Android 中View的绘制机制源代码分析 三

    到眼下为止,measure过程已经解说完了,今天開始我们就来学习layout过程.只是在学习layout过程之前.大家有没有发现我换了编辑器,哈哈.最终下定决心从Html编辑器切换为markdown编 ...

  7. Android中实现ListView圆角效果[转]

    本文演示如何Android中实现ListView圆角效果. 无论是网站,还是APP,人们都爱看一些新颖的视图效果.直角看多了,就想看看圆角,这几年刮起了一阵阵的圆角设计风:CSS新标准纳入圆角元素,特 ...

  8. Android 中View的绘制机制源代码分析 一

    尊重原创: http://blog.csdn.net/yuanzeyao/article/details/46765113 差点儿相同半年没有写博客了,一是由于工作比較忙,二是认为没有什么内容值得写, ...

  9. Android 中View的绘制机制源代码分析 二

    尊重原创:http://blog.csdn.net/yuanzeyao/article/details/46842891 本篇文章接着上篇文章的内容来继续讨论View的绘制机制,上篇文章中我们主要解说 ...

随机推荐

  1. Asp.net 主题

    设定主题: 右击网站,选择添加ASP.NET文件夹,选择主题.系统默认将文件夹命名为App_Themes,我们在这个文件夹下添加外观文件,在.skin后缀的文件中自定义我们想要的主题. 例如: < ...

  2. JavaScript的“闭包”到底是什么(2)

    我的上篇博客标题不对,造成一些误解.我认为博客的宗旨不是背教科书,而是分享研发心得.我的上篇标题因该改成“JavaScript 闭包的一个议题:它对outer scope 的影响”,因为我没有严格地去 ...

  3. 实时错误 '91' :对象变量或with块变量未设置

    大家这几天在做学生信息管理系统的时候,出现最多的应该就是这个问题了,“实时错误‘91’:对象变量或with块变量未设置”.如右图: 遇到这个问题,我们首先应该去参考MSDN,不过这时候MSDN似乎没有 ...

  4. Objective-C内存管理与原理

    尽管苹果在 iOS 5/ Mac OS X 10.7 开始导入ARC,利用 Xcode4.2 可以使用该机能.ARC就是自动引用计数,是一项为Objective - C程序在编译时提供自动内存管理的功 ...

  5. 探讨CMake中关于RPATH的使用

    最近研究CMake,发现CMake对于RPATH的管理也非常人性化.官方说法是当动态库的编译也和执行档在同级目录下的时候,CMake会自动给执行档加入适当的RPATH.具体可以通过readelf -d ...

  6. c语言Winpcap编程构造并接收解析arp包

    /* 程序功能: 1.构造arp包,并发送.程序参数顺序:源IP.目的IP.mac地址.flag 2.获取网络中的ARP数据包,解析数据包的内容.程序参数:日志文件名 winpacp中文技术文档(基本 ...

  7. codeforces567E. President and Roads

    题目大意:总统要回家,会经过一些街道,每条街道都是单向的并且拥有权值.现在,为了让总统更好的回家,要对每一条街道进行操作:1)如果该街道一定在最短路上,则输出“YES”.2)如果该街道修理过后,该边所 ...

  8. git的使用说明详解

    http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001375840202368c7 ...

  9. laravel框架——保存用户登陆信息(session)

    public function inlog(Request $request) { //获取表单提交的数据 $input = $request->all(); //根本获取的数据去数据库中查询 ...

  10. Delphi 缩放图像代码 - 支持PNG透明通道(利用了Windows的windowscodecs.dll)

    要求Delphi2007或者更高版本, 系统要求至少XP-SP2以上 实际上是利用了Windows的windowscodecs.dll这个文件的功能 在VCL里已经封装为TWICImage类 proc ...