Android中使用ListView绘制自定义表格(2)
上回再写了《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";//行的样式id
- public 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;
- }
- }
源码下载地址点击打开链接
Android中使用ListView绘制自定义表格(2)的更多相关文章
- Android中使用ListView实现自适应表格
GridView比ListView更容易实现自适应的表格,但是GridView每个格单元的大小固定,而ListView实现的表格可以自定义每个格单元的大小,但因此实现自适应表格也会复杂些(格单元大小不 ...
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
Android中View的绘制过程 onMeasure方法简述 附有自定义View例子 Android中View的绘制过程 当Activity获得焦点时,它将被要求绘制自己的布局,Android fr ...
- 【转】Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
Android中View的绘制过程 当Activity获得焦点时,它将被要求绘制自己的布局,Android framework将会处理绘制过程,Activity只需提供它的布局的根节点. 绘制过程从布 ...
- 【转】整理一下Android中的ListView
原文网址:http://sunbofu.blog.51cto.com/6431507/1280441 Android中的listview目测是一个使用频率很高的组件,所以今天来总结一下listview ...
- Android中的ListView属性使用总结
Android中使用ListView控件比较常见,如果能知道常用的一些属性使用,肯定会少很多坑. 1.ListView是常用的显示控件,默认背景是和系统窗口一样的透明色,如果给ListView加上背景 ...
- Android 中View的绘制机制源代码分析 三
到眼下为止,measure过程已经解说完了,今天開始我们就来学习layout过程.只是在学习layout过程之前.大家有没有发现我换了编辑器,哈哈.最终下定决心从Html编辑器切换为markdown编 ...
- Android中实现ListView圆角效果[转]
本文演示如何Android中实现ListView圆角效果. 无论是网站,还是APP,人们都爱看一些新颖的视图效果.直角看多了,就想看看圆角,这几年刮起了一阵阵的圆角设计风:CSS新标准纳入圆角元素,特 ...
- Android 中View的绘制机制源代码分析 一
尊重原创: http://blog.csdn.net/yuanzeyao/article/details/46765113 差点儿相同半年没有写博客了,一是由于工作比較忙,二是认为没有什么内容值得写, ...
- Android 中View的绘制机制源代码分析 二
尊重原创:http://blog.csdn.net/yuanzeyao/article/details/46842891 本篇文章接着上篇文章的内容来继续讨论View的绘制机制,上篇文章中我们主要解说 ...
随机推荐
- JavaScript--模拟网络爬虫
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- SQL中varchar和nvarchar有什么区别?
varchar(n)长度为 n 个字节的可变长度且非 Unicode 的字符数据.n 必须是一个介于 1 和 8,000 之间的数值.存储大小为输入数据的字节的实际长度,而不是 n 个字节.nvarc ...
- 安装SVN及实现nginx web同步更新需要在WDCP一键安装包的基础上
一.安装 1.查看是否安装cvs rpm -qa | grep subversion 2.安装 yum install subversion 3.测试是否安装成功 /usr/bin/svnserve ...
- JQuery语法总结和注意事项
1.关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用do ...
- MapReduce概念(转)
昨天,我在Xebia印度办公室发表了一个关于MapReduce的演说.演说进行得很顺利,听众们都能够理解MapReduce的概念(根据他们的反馈).我成功地向技术听众们(主要是Java程序员,一些Fl ...
- Yii2的相关学习记录,初始化Yii2(二)
前面已经将Yii2下载下来了,那我们就需要能实际的使用. 一.初始化,因为我都是在windows系统下,所以用cmd命令打开下载下来的Yii2的根目录.然后运行下面命令: init 会提示选择0为开发 ...
- checkbox复选框全选批量删除
多选框全选实现批量删除 html代码 <body> <form action="" method="post" name="Form ...
- python运维开发之第五天
一.模块五大部分 定义,导入方法,import本质(路径搜索),导入优化,模块的分类 1.定义: 模块是用来从逻辑上组织python代码(变量,函数,类,逻辑实现一个功能),本质就是.py结尾的pyt ...
- C语言笔记(数组地址一些细节)
一.数组的a+1和&a+1的区别 先看看测试代码: ]={}; printf(" sizeof(data) = %d.\n", sizeof(data)); printf ...
- JavaScript 类型判断的那些事
先准备几个变量 var a = "abcde."; var b = 222; var c= [1,2,3]; // 或者 new Array() var d = new Date( ...