当前位置: 编程技术>移动开发
Android中使用ListView绘制自定义表格技巧分享
来源: 互联网 发布时间:2014-10-18
本文导语: 先上一下可以实现的效果图 要实现的效果有几方面 1、列不固定:可以根据数据源的不同生成不同的列数 2、表格内容可以根据数据源的定义合并列 3、要填写的单元格可以选择自定义键盘还是系统键盘 奔着这三点,做了个...
先上一下可以实现的效果图
要实现的效果有几方面
1、列不固定:可以根据数据源的不同生成不同的列数
2、表格内容可以根据数据源的定义合并列
3、要填写的单元格可以选择自定义键盘还是系统键盘
奔着这三点,做了个简单的实现,把源码贴一下(因为该点是主界面中的一部分,不便于放整个Demo)
自定义适配器,CallBackInterface是自定义的回调接口,这里定义回调是因为数据输入时需要及时保存
public class SiteDetailViewAdapter extends BaseAdapter implements CallBackInterface{
private Context context;
private LayoutInflater inflater;
private ArrayList lists;
private KeyBoard keyBoard = null;//自定义键盘
private ListView listView = null;
private boolean isReadOnly = false;//是否是浏览状态
private String[] arrCellType = null;
private int[] arrHeadWidth = null;//每列宽度
public SiteDetailViewAdapter(Context context, ArrayList lists
,KeyBoard keyBoard,ListView listView,boolean isReadOnly
,int[] arrHeadWidth) {
super();
this.context = context;
this.lists = lists;
inflater = LayoutInflater.from(context);
this.keyBoard = keyBoard;
this.listView = listView;
this.isReadOnly = isReadOnly;
this.arrHeadWidth = arrHeadWidth;
this.listView.setAdapter(this);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return lists.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int index, View view, ViewGroup arg2) {
HashMap map = lists.get(index);
String type = (String)map.get("rowtype");
ArrayList itemCells = new ArrayList();
//String cellValue,String cellKey,long cellType,int cellInRow,int cellSpan
ItemCell itemCellXuHao = new ItemCell((index+1)+"","-1",1,-1,1);
itemCells.add(itemCellXuHao);
for(int i=0;i
要实现的效果有几方面
1、列不固定:可以根据数据源的不同生成不同的列数
2、表格内容可以根据数据源的定义合并列
3、要填写的单元格可以选择自定义键盘还是系统键盘
奔着这三点,做了个简单的实现,把源码贴一下(因为该点是主界面中的一部分,不便于放整个Demo)
自定义适配器,CallBackInterface是自定义的回调接口,这里定义回调是因为数据输入时需要及时保存
代码如下:
public class SiteDetailViewAdapter extends BaseAdapter implements CallBackInterface{
private Context context;
private LayoutInflater inflater;
private ArrayList lists;
private KeyBoard keyBoard = null;//自定义键盘
private ListView listView = null;
private boolean isReadOnly = false;//是否是浏览状态
private String[] arrCellType = null;
private int[] arrHeadWidth = null;//每列宽度
public SiteDetailViewAdapter(Context context, ArrayList lists
,KeyBoard keyBoard,ListView listView,boolean isReadOnly
,int[] arrHeadWidth) {
super();
this.context = context;
this.lists = lists;
inflater = LayoutInflater.from(context);
this.keyBoard = keyBoard;
this.listView = listView;
this.isReadOnly = isReadOnly;
this.arrHeadWidth = arrHeadWidth;
this.listView.setAdapter(this);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return lists.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int index, View view, ViewGroup arg2) {
HashMap map = lists.get(index);
String type = (String)map.get("rowtype");
ArrayList itemCells = new ArrayList();
//String cellValue,String cellKey,long cellType,int cellInRow,int cellSpan
ItemCell itemCellXuHao = new ItemCell((index+1)+"","-1",1,-1,1);
itemCells.add(itemCellXuHao);
for(int i=0;i