Android中使用ListView绘制自定义表格(2)

时间:2023-03-08 20:12:07

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

效果图如

Android中使用ListView绘制自定义表格(2)Android中使用ListView绘制自定义表格(2)

一、功能:

1、支持列合并

2、考虑了界面刷新优化

3、预留部分接口

4、支持左右滚动

1、枚举类:CellTypeEnum

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

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

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

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

  1. package ****.danielinbiti.custometableview.item;
  2. import java.util.ArrayList;
  3. import ****.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. }

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