上回再写了《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. SOA与EBS集成_Package 调用维护

    介绍SOA Suite与EBS集成成,使用PACKAGE调用集成的组合程序的运维方法: 1.打开组合程序,在oracle adapter上右键,选择edit,在配置向导中跳转到第五步,确认组合程序调用 ...

  2. [转]Java远程方法调用

    Java远程方法调用,即Java RMI(Java Remote Method Invocation)是Java编程语言里,一种用于实现远程过程调用的应用程序编程接口.它使客户机上运行的程序可以调用远 ...

  3. SQL Database学习笔记

    1. linux下快速安装MariaDB: MariaDB 是 一个采用 Maria 存储引擎的  MySQL  分支版本,是由原来 MySQL 的作者 Michael Widenius 创办的公司所 ...

  4. (转载)最实用的清除浮动代码 css的文字过长裁剪后面跟着省略号

    css: .clearfloat:after{display:block;clear:both;content:"";visibility:hidden;} .clearfloat ...

  5. [CentOS 0010] CentOS 配置mysql允许远程登录

    Mysql为了安全性,在默认情况下用户只允许在本地登录,可是在有此情况下,还是需要使用用户进行远程连接,因此为了使其可以远程需要进行如下操作: 一.允许root用户在任何地方进行远程登录,并具有所有库 ...

  6. POJ1505 Copying Books(二分法)

    B - 二分 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description   Be ...

  7. Memcached源码分析——hash

    以下为memcached中关于使用的hash算法的一点记录 memcached中默认使用的是Bob Jenkins的jenkins_hash算法 以下4段代码均在memcached-1.4.22/ha ...

  8. RESTful API -备

    网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......). 因此,必须有一种统一的机制,方便不同的前端设备与后端进行通信.这导致AP ...

  9. linux wenjian

    文件锁是一种文件读写机制,在任何特定的时间只允许一个进程访问一个文件.利用这种机制能够使读写单个文件的过程变得更安全. 在这篇文章中,我们将探讨Linux中不同类型的文件锁,并通过示例程序来理解它们之 ...

  10. BZOJ1709: [Usaco2007 Oct]Super Paintball超级弹珠

    1709: [Usaco2007 Oct]Super Paintball超级弹珠 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 324  Solved: ...