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的绘制机制,上篇文章中我们主要解说 ...
随机推荐
- SOA与EBS集成_Package 调用维护
介绍SOA Suite与EBS集成成,使用PACKAGE调用集成的组合程序的运维方法: 1.打开组合程序,在oracle adapter上右键,选择edit,在配置向导中跳转到第五步,确认组合程序调用 ...
- [转]Java远程方法调用
Java远程方法调用,即Java RMI(Java Remote Method Invocation)是Java编程语言里,一种用于实现远程过程调用的应用程序编程接口.它使客户机上运行的程序可以调用远 ...
- SQL Database学习笔记
1. linux下快速安装MariaDB: MariaDB 是 一个采用 Maria 存储引擎的 MySQL 分支版本,是由原来 MySQL 的作者 Michael Widenius 创办的公司所 ...
- (转载)最实用的清除浮动代码 css的文字过长裁剪后面跟着省略号
css: .clearfloat:after{display:block;clear:both;content:"";visibility:hidden;} .clearfloat ...
- [CentOS 0010] CentOS 配置mysql允许远程登录
Mysql为了安全性,在默认情况下用户只允许在本地登录,可是在有此情况下,还是需要使用用户进行远程连接,因此为了使其可以远程需要进行如下操作: 一.允许root用户在任何地方进行远程登录,并具有所有库 ...
- POJ1505 Copying Books(二分法)
B - 二分 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description Be ...
- Memcached源码分析——hash
以下为memcached中关于使用的hash算法的一点记录 memcached中默认使用的是Bob Jenkins的jenkins_hash算法 以下4段代码均在memcached-1.4.22/ha ...
- RESTful API -备
网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......). 因此,必须有一种统一的机制,方便不同的前端设备与后端进行通信.这导致AP ...
- linux wenjian
文件锁是一种文件读写机制,在任何特定的时间只允许一个进程访问一个文件.利用这种机制能够使读写单个文件的过程变得更安全. 在这篇文章中,我们将探讨Linux中不同类型的文件锁,并通过示例程序来理解它们之 ...
- BZOJ1709: [Usaco2007 Oct]Super Paintball超级弹珠
1709: [Usaco2007 Oct]Super Paintball超级弹珠 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 324 Solved: ...