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

本文介绍了一种在Android中实现自定义TableView的方法,支持列合并、界面刷新优化及左右滚动等功能。通过枚举类定义样式,并提供了核心代码CustomeTableItem实现ListView的行item。

上回再写了《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;
	}
}


源码下载地址点击打开链接







内容概要:本文介绍了一个关于超声谐波成像中幅度调制聚焦超声所引起全场位移和应变的分析模型,并提供了基于Matlab的代码实现。该模型旨在精确模拟和分析在超声谐波成像过程中,由于幅度调制聚焦超声作用于生物组织时产生的力学效应,包括全场的位移与应变分布,从而为医学成像和治疗提供理论支持和技术超声谐波成像中幅度调制聚焦超声引起的全场位移和应变的分析模型(Matlab代码实现)手段。文中详细阐述了模型构建的物理基础、数学推导过程以及Matlab仿真流程,具有较强的理论深度与工程应用价值。; 适合人群:具备一定声学、生物医学工程或力学背景,熟悉Matlab编程,从事医学成像、超声技术或相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于超声弹性成像中的力学建模与仿真分析;②支持高强度聚焦超声(HIFU)治疗中的组织响应预测;③作为教学案例帮助理解超声与组织相互作用的物理机制;④为相关科研项目提供可复用的Matlab代码框架。; 阅读建议:建议读者结合超声物理和连续介质力学基础知识进行学习,重点关注模型假设、偏微分方程的数值求解方法及Matlab实现细节,建议动手运行并修改代码以加深理解,同时可拓展应用于其他超声成像或治疗场景的仿真研究。
### 关于PAT Basic Level Practice的测试点及题目解析 #### 题目难度分级 PAT(Programming Ability Test)是由浙江大学举办的计算机程序设计能力考试,分为不同级别。其中乙级即Basic Level主要面向初学者,考察基本编程技能[^1]。 #### 测试点特点 对于PAT Basic Level中的某些特定题目而言,其测试点设置较为严格。例如,在处理字符串匹配类问题时,需要注意算法逻辑中何时应当终止循环以防止不必要的重复计算;而在涉及数值运算的问题里,则可能因为边界条件而增加复杂度[^3]。 #### 编程语言的选择影响 值得注意的是,尽管大部分简单题目可以作为学习某种新语言的良好实践材料,但在实际操作过程中可能会遇到由于所选语言特性而导致难以通过全部测试点的情况。比如Java在面对部分效率敏感型试题时表现不佳,这可能是由于该语言本身的执行速度相对较慢以及内存管理方式等因素造成的。因此有时不得不转而采用其他更适合解决此类问题的语言版本来完成解答[^2]。 ```cpp #include<bits/stdc++.h> using namespace std; int a[100000]; int c=1; void getPrime(){ int flag=0; for(int i=2;i<105000;i++){ flag=1; for(int j=2;j<=sqrt(i);j++){ if(i%j==0){ flag=0; break; } } if(flag==1) a[c++]=i; } } int main(){ int m,n,i,t=1; scanf("%d %d",&m,&n); getPrime(); for(i=m;i<=n;i++){ if(t%10==1){ printf("%d",a[i]); t++; }else{ printf(" %d",a[i]); t++; } if((t-1)%10==0) printf("\n"); } return 0; } ``` 上述C++代码展示了如何实现一个简单的质数打印功能,并且针对输出格式进行了特殊处理以满足特定要求。这段代码很好地体现了编写高效解决方案的重要性,尤其是在应对像PAT这样的在线评测系统时[^4]。
评论 12
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值