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.ListVIEw,this.keyBoard,this);
vIEw.setTag(itemCustom);
}else{
ListItemCustom itemCustom = (ListItemCustom)vIEw.getTag();
itemCustom.refreshData(itemCells,index);
}
if(index%2 == 0){
vIEw.setBackgroundcolor(color.argb(250,255,255 ));
}else{
vIEw.setBackgroundcolor(color.argb(250,224,243,250 ));
}
return vIEw;
}
@OverrIDe
public boolean exectueMethod(Object params) {
String[] arr = (String[])params;
HashMap map = Lists.get(Integer.parseInt(arr[0]));
ItemCell itemCell = (ItemCell)map.get(arr[1]);
itemCell.setCellValue(arr[2]);
itemCell.setIsChange(true);
return false;
}
ListVIEw每行的布局,内部代码是有冗余的,因为是Demo,所以先以效果未准,未进行代码重构,注意不能往ListItemCustom中传入每行的Map来进行值得获取或者输入更新等 *** 作,因为Map是按地址方式,再结合ListVIEw的绘制方式,最终的map不是你想象的map。
复制代码 代码如下:
public class ListItemCustom extends linearLayout{
public ListItemCustom(Context context){
super(context);
}
public ListItemCustom(Context context,AttributeSet attrs) {
super(context,attrs);
}
private String type = "-1";
private Context context = null;
private boolean isRead = false;
private int[] headWIDthArr = null;
private ListVIEw ListVIEw = null;
private KeyBoard keyBoard = null;
private int orderNum = -1;
private ArrayList<VIEw> vIEwList = new ArrayList();
private int rowNum = -1;
private CallBackInterface callBack = null;
public voID buildItem(String type,Context context,boolean isRead,int[] headWIDthArr
,ArrayList<ItemCell> itemCells,int rowNum
,CallBackInterface callBack){
this.context = context;
this.isRead = isRead;
this.headWIDthArr = headWIDthArr;
this.setorIEntation(linearLayout.VERTICAL);
this.rowNum = rowNum;
this.ListVIEw = ListVIEw;
this.keyBoard = keyBoard;
this.callBack = callBack;
this.type = type;
this.setLayoutParams(new linearLayout.LayoutParams(LayoutParams.MATCH_PARENT,45));
this.addCell(itemCells);
}
public voID refreshData(ArrayList<ItemCell> itemCells,int rowNum){
this.rowNum = rowNum;
this.refreshCells(itemCells);
}
private voID refreshCells(ArrayList<ItemCell> itemCells){
for(int i=0;i<itemCells.size();i++){
ItemCell itemCell = itemCells.get(i);
if(itemCell.getCellType()==ItemCellValueType.VALUE_NONE){
TextVIEw vIEw = (TextVIEw)vIEwList.get(i);
vIEw.setID(itemCell.getCellID());
vIEw.setText(itemCell.getCellValue());
}else if(itemCell.getCellType()==ItemCellValueType.VALUE_NUMBER){
EditText vIEw= (EditText)vIEwList.get(i);
vIEw.setID(itemCell.getCellID());
vIEw.setText(itemCell.getCellValue());
//vIEw.setText(itemCell.getCellID()+"");
//vIEw.setTag(itemCell.getCellKey()+"");
this.setEditVIEw(vIEw,itemCell.getCellKey());
this.setonKeyBorad(vIEw,itemCell.getCellinRow());
}else if(itemCell.getCellType()==ItemCellValueType.VALUE_STRING){
EditText vIEw= (EditText)vIEwList.get(i);
vIEw.setID(itemCell.getCellID());
vIEw.setText(itemCell.getCellValue());
//vIEw.setText(itemCell.getCellID()+"");
//vIEw.setTag(itemCell.getCellKey()+"");
this.setEditVIEw(vIEw,itemCell.getCellKey());
}
}
}
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 voID addCell(ArrayList<ItemCell> itemCells){
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()==ItemCellValueType.VALUE_NONE){
TextVIEw vIEw = (TextVIEw)getReadVIEw();
vIEw.setID(itemCell.getCellID());
vIEw.setText(itemCell.getCellValue());
vIEw.setWIDth(wIDth);
secondLayout.addVIEw(vIEw);
vIEwList.add(vIEw);
}else if(itemCell.getCellType()==ItemCellValueType.VALUE_NUMBER){
EditText vIEw= (EditText)getinputVIEw();
vIEw.setID(itemCell.getCellID());
vIEw.setText(itemCell.getCellValue());
vIEw.setWIDth(wIDth);
//vIEw.setText(itemCell.getCellID()+"");
//vIEw.setTag(itemCell.getCellKey()+"");
this.setEditVIEw(vIEw,itemCell.getCellinRow());
secondLayout.addVIEw(vIEw);
vIEwList.add(vIEw);
}else if(itemCell.getCellType()==ItemCellValueType.VALUE_STRING){
EditText vIEw= (EditText)getinputVIEw();
vIEw.setID(itemCell.getCellID());
vIEw.setText(itemCell.getCellValue());
vIEw.setWIDth(wIDth);
//vIEw.setText(itemCell.getCellID()+"");
//vIEw.setTag(itemCell.getCellKey()+"");
this.setEditVIEw(vIEw,itemCell.getCellKey());
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);
}
}
}
private VIEw getVerticalline(){
return LayoutInflater.from(context).inflate(R.layout.atom_line_v_vIEw,null);
}
private VIEw getReadVIEw(){
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 setonKeyBorad(EditText edtText1,int index){
if(!this.isRead){
onListenText(edtText1,index);
}
}
private voID onListenText(EditText edtText,int index){
final ListVIEw lsv = ListVIEw;
final int IDx = index;
edtText.setonFocuschangelistener(new OnFocuschangelistener(){
@OverrIDe
public voID onFocusChange(VIEw arg0,boolean arg1) {
if(arg1){
int[] y = getYPos(lsv,IDx);
keyBoard.show((EditText)arg0,y[0],y[1]);
}
}
});
}
private int[] getYPos(ListVIEw ListvIEw,int index){
int[] yPosArr = new int[2];
Rect r = new Rect();
ListvIEw.getChildAt(0).getGlobalVisibleRect(r);
int first = ListvIEw.getFirstVisibleposition();
yPosArr[1] = (index-first+1)*r.height()+ListvIEw.gettop();
yPosArr[0] = (index-first)*r.height()+ListvIEw.gettop();
return yPosArr;
}
private voID setEditVIEw(EditText edtText1,final String key){
if(this.isRead){
edtText1.setEnabled(false);
}else{
edtText1.addTextChangedListener(new TextWatcher() {
@OverrIDe
public voID afterTextChanged(Editable arg0) {
String[] arr = new String[3];
arr[0] = rowNum+"";
arr[1] = key;
arr[2] = arg0.toString();
callBack.exectueMethod(arr);
// ItemCell itemCell = (ItemCell)dataMap.get(key);
// itemCell.setCellValue(arg0.toString());
// itemCell.setIsChange(true);
}
@OverrIDe
public voID beforeTextChanged(CharSequence arg0,int arg1,int arg2,
int arg3) {
}
@OverrIDe
public voID onTextChanged(CharSequence arg0,
int arg3) {
}
});
}
}
public String getType(){
return this.type;
}
}
复制代码 代码如下:
public class ItemCell {
private String cellValue = "";
private int cellSpan = 1;
private String cellKey = "";
private int cellinRow = 0;
private long cellType = ItemCellValueType.VALUE_NONE;
private int colNum = 0;
private int rowType = 0;
private int cellID = -1;
private boolean isValueFromtable = false;
private boolean isChange = false;
public ItemCell(String cellValue,int cellSpan){
this.cellValue = cellValue;
this.cellType = cellType;
this.cellSpan = cellSpan;
this.cellKey = cellKey;
this.cellinRow = cellinRow;
}
public ItemCell(String cellValue,int cellinRow){
this(cellValue,cellKey,cellType,cellinRow,1);
}
public voID setColNum(int colNum){
this.colNum = colNum;
}
public int getColNum(){
return this.colNum;
}
public voID setRowType(int rowType){
this.rowType = rowType;
}
public int getRowType(){
return this.rowType;
}
public String getCellValue(){
return cellValue;
}
public voID setCellValue(String value){
this.cellValue = value;
}
public long getCellType(){
return cellType;
}
public int getCellSpan(){
return cellSpan;
}
public String getCellKey(){
return cellKey;
}
public int getCellinRow(){
return cellinRow;
}
public voID setIsChange(boolean isChange){
this.isChange = isChange;
}
public boolean getIsChange(){
return this.isChange;
}
public int getCellID() {
return cellID;
}
public voID setCellID(int cellID) {
this.cellID = cellID;
}
public boolean isValueFromtable() {
return isValueFromtable;
}
public voID setValueFromtable(boolean isValueFromtable) {
this.isValueFromtable = isValueFromtable;
}
}
custome_List_item.xml文件
复制代码 代码如下:
<?xml version="1.0" enCoding="utf-8"?>
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"
androID:layout_wIDth="match_parent"
androID:layout_height="40dp"
androID:orIEntation="vertical"
androID:ID="@+ID/test_dajej"
androID:background="#ffffff">
<srit.collection.Widget.costomtable.ListItemCustom androID:ID="@+ID/custome_item"
androID:layout_wIDth="fill_parent"
androID:layout_height="wrap_content"
/>
</linearLayout>
以上是核心的文件内容。有了列合并,行合并也不远了。可以在自定义的布局中多加个linearLayout来实现行合并。 总结
以上是内存溢出为你收集整理的Android中使用ListView绘制自定义表格技巧分享全部内容,希望文章能够帮你解决Android中使用ListView绘制自定义表格技巧分享所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)