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

本文介绍如何在Android中使用ListView创建动态列数、支持列合并的自定义表格,并详细说明了实现过程。通过自定义适配器和回调接口,实现了数据输入时的实时保存功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先上一下可以实现的效果图 



要实现的效果有几方面

1、列不固定:可以根据数据源的不同生成不同的列数

2、表格内容可以根据数据源的定义合并列

3、要填写的单元格可以选择自定义键盘还是系统键盘


奔着这三点,做了个简单的实现,把源码贴一下(因为该点是主界面中的一部分,不便于放整个Demo)

自定义适配器,CallBackInterface是自定义的回调接口,这里定义回调是因为数据输入时需要及时保存

public class SiteDetailViewAdapter extends BaseAdapter implements CallBackInterface{
	private Context context;
	private LayoutInflater inflater;
	private ArrayList<HashMap<String,Object>> 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<HashMap<String,Object>> 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<ItemCell> 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<map.size()-1;i++){
			ItemCell itemCell = (ItemCell)map.get(i+"");
			itemCells.add(itemCell);
		}
		//性能优化后需要放开注释danielinbiti
		if(view == null||view!=null&&!((ListItemCustom)view.getTag()).getType().equals(type)){
			view = inflater.inflate(R.layout.customel_list_item, null);
			ListItemCustom itemCustom = (ListItemCustom)view.findViewById(R.id.custome_item);
			itemCustom.buildItem(type, context, isReadOnly,arrHeadWidth,itemCells,index
					,this.listV
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值