Android中使用ListView绘制自定义表格(2)
来源:互联网 发布:java中io流总结 编辑:程序博客网 时间:2024/04/29 09:24
上回再写了《Android中使用ListView绘制自定义表格》后,很多人留言代码不全和没有数据样例。但因为项目原因,没法把源码全部贴上来。近两天,抽空简化了一下,做了一个例子。
效果图如
一、功能:
1、支持列合并
2、考虑了界面刷新优化
3、预留部分接口
4、支持左右滚动
1、枚举类:CellTypeEnum
package csdn.danielinbiti.custometableview.item;public enum CellTypeEnum { STRING //字符 ,DIGIT //数字 ,LABEL //标签}该类定义了表格支持的样式,可以根据需要扩充,扩充了新类型,注意同时修改CustomeTableItem中新类型样式的创建方式
2、核心代码CustomeTableItem,该类对应ListView的一行item。类支持列合并,没有实现行合并(行合并样式控制上会比较复杂些,如有需要自己改写吧)
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";//行的样式idpublic 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);}/* * rowType:行的样式,字符任意,相同样式的行不需要再创建了 * itemCells:单元格信息 * headWidthArr:每列宽度 * isRead:是否只读,如果是只读,则所有的输入都不生效 */ 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;}}
源码下载地址点击打开链接
5 0
- Android中使用ListView绘制自定义表格
- Android中使用ListView绘制自定义表格(2)
- Android中使用ListView绘制自定义表格(3)
- Android中使用 listView 绘制表格
- 在android 自定义listView中绘制矩形
- Android中使用ListView实现自适应表格
- Android使用RecyclerView绘制表格
- android 使用TableLayout 绘制表格
- Android中使用ListView实现漂亮的表格效果
- Android中自定义ListView
- Android中自定义ListView
- Android中自定义ListView
- 使用listview绘制表格,实现双击表头排序
- Android中自定义View中的使用与绘制技巧
- Android中使用自定义View实现shape图形绘制
- Android 对Layout_weight属性完全解析以及使用ListView来实现表格(自定义适配器)
- android listview 中 item显示表格样式
- Android 在slidingMenu中的ListFrament中使用自定义的ListView
- JAVA反射机制
- Android菜鸟的成长笔记(25)——可爱的小闹钟
- day3,page25,total105
- ExtJS4.2第一天---数据模型
- w3c系列之CSS(三):CSS属性是如何被应用到元素上的
- Android中使用ListView绘制自定义表格(2)
- 九度oj 题目1008:最短路径问题
- COM线程模型 - MTA接口 (传递MTA COM对象给STA套间线程)
- Lesson 4 Abstraction in computer science
- 如何修改MTK 6592 手机(如果华为荣耀3X系列)IMEI码
- 目标检测的图像特征提取之(二)LBP特征
- java内置锁synchronized的可重入性
- Android入门:Intent入门(1)
- ring3通过METHOD_BUFFERED、METHOD_IN_DIRECT两种常用方式和ring0级交互读文件测试