
我是一个.net程序员,但是苦于公司要求开发一个androID app,没办法,只能硬着头皮上了。
由于项目里面很多地方需要用到数据显示控件(类似于.net的DataGrIDVIEw),度娘找了下发现没人公开类似的控件,没办法只好自己写了。
废话不多说,直接贴代码:
public class DataGrIDVIEw extends horizontalscrollview { private List<DataGrIDVIEwColumn> columns; private List<Map<String,String>> rows; private boolean hasheader; private CellClickListener cellClickListener; private RowClickListener rowClickListener; private RowValIDatorListener rowValIDatorListener; private linearLayout headerRow; private linearLayout bodyRow; public DataGrIDVIEw(Context context,AttributeSet attrs) { super(context,attrs); TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.DataGrIDVIEw); hasheader = a.getBoolean(R.styleable.DataGrIDVIEw_hasheader,true); a.recycle(); LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); linearLayout container = (linearLayout) inflater.inflate(R.layout.ctrl_data_grID_vIEw,null,false); addVIEw(container); this.columns = new ArrayList<DataGrIDVIEwColumn>(); this.rows = new ArrayList<Map<String,String>>(); headerRow = new linearLayout(getContext()); headerRow.setorIEntation(linearLayout.HORIZONTAL); headerRow.setBackgroundResource(R.drawable.datagrID_header_background); headerRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)); if (hasheader){ container.addVIEw(headerRow); } ScrollVIEw scrollVIEw = (ScrollVIEw)inflater.inflate(R.layout.ctrl_data_grID_vIEw_scroll,container,false); bodyRow = (linearLayout) inflater.inflate(R.layout.ctrl_data_grID_vIEw,scrollVIEw,false); scrollVIEw.addVIEw(bodyRow); container.addVIEw(scrollVIEw); } public voID addColumn(String datafield,String headerText){ this.addColumn(datafield,headerText,200); } public voID addColumn(String datafield,String headerText,int columnWIDth){ this.addColumn(datafield,columnWIDth,Gravity.START); } public voID addColumn(String datafield,int columnWIDth,int textAlign){ this.addColumn(datafield,textAlign,color.rgb(80,80,80)); } public voID addColumn(String datafield,int textAlign,int textcolor){ this.addColumn(datafield,textcolor,true); } public voID addColumn(String datafield,int textcolor,boolean isEnabled){ DataGrIDVIEwColumn column = new DataGrIDVIEwColumn(); column.datafield =datafield; column.headerText = headerText; column.columnWIDth = columnWIDth; column.textAlign = textAlign; column.textcolor = textcolor; column.isEnabled = isEnabled; this.addColumn(column); } public voID addColumn(DataGrIDVIEwColumn column){ columns.add(column); insertColumn(column); if (rows.size() > 0){ bodyRow.removeAllVIEws(); for (Map<String,String> row : rows){ insertRow(row); } } } public voID addColumn(DataGrIDVIEwColumn column,int index){ columns.add(column); insertColumn(column,index); if (rows.size() > 0){ bodyRow.removeAllVIEws(); for (Map<String,String> row : rows){ insertRow(row); } } } public voID removeColumn(int index){ columns.remove(index); } public voID removeColumn(String datafield){ for (DataGrIDVIEwColumn column : columns){ if (column.datafield.equals(datafield)){ this.removeColumn(column); if (rows.size() > 0){ bodyRow.removeAllVIEws(); for (Map<String,String> row : rows){ insertRow(row); } } return; } } } public voID removeColumn(DataGrIDVIEwColumn column){ columns.remove(column); } public voID setDataSource(List<Map<String,String>> rows){ this.rows = rows; if (columns.size() > 0){ bodyRow.removeAllVIEws(); for (Map<String,String> row : rows){ insertRow(row); } } } public voID addRow(Map<String,String> row){ rows.add(row); if (columns.size() > 0) { insertRow(row); } } public voID addRow(Map<String,String> row,int index){ rows.add(index,row); if (columns.size() > 0) { insertRow(row,index); } } public voID removeRow(int index){ rows.remove(index); bodyRow.removeVIEwAt(index); } public voID removeRow(Map<String,String> row){ int index = rows.indexOf(row); this.removeRow(index); } public voID setCellClickListener(CellClickListener cellClickListener) { this.cellClickListener = cellClickListener; } public voID setRowClickListener(RowClickListener rowClickListener) { this.rowClickListener = rowClickListener; } public voID setRowValIDatorListener(RowValIDatorListener rowValIDatorListener) { this.rowValIDatorListener = rowValIDatorListener; } public boolean isHasheader() { return hasheader; } public voID setHasheader(boolean hasheader) { this.hasheader = hasheader; } private voID insertColumn(DataGrIDVIEwColumn column){ this.insertColumn(column,-1); } private voID insertColumn(DataGrIDVIEwColumn column,int index){ LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); TextVIEw headerTextVIEw = (TextVIEw) inflater.inflate(R.layout.ctrl_data_grID_vIEw_column,headerRow,false); headerTextVIEw.setText(column.headerText); headerTextVIEw.setLayoutParams(new LayoutParams(column.columnWIDth,LayoutParams.WRAP_CONTENT,1)); if (index == -1){ headerRow.addVIEw(headerTextVIEw); }else { headerRow.addVIEw(headerTextVIEw,index); } } public DataGrIDVIEwColumn getColumn(int index){ return columns.get(index); } private voID insertRow(final Map<String,String> row){ this.insertRow(row,-1); } private voID insertRow(final Map<String,int index){ linearLayout daTarow = new linearLayout(getContext()); daTarow.setorIEntation(linearLayout.HORIZONTAL); daTarow.setSelected(true); daTarow.setClickable(true); daTarow.setBackgroundResource(R.drawable.datagrID_row_border); daTarow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)); LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); for (final DataGrIDVIEwColumn column : columns){ String cellText = row.get(column.datafield); TextVIEw rowFIEldText = (TextVIEw) inflater.inflate(R.layout.ctrl_data_grID_vIEw_cell,daTarow,false); rowFIEldText.setText(cellText); rowFIEldText.setGravity(column.textAlign); rowFIEldText.setTextcolor(column.textcolor); rowFIEldText.setLayoutParams(new LayoutParams(column.columnWIDth,1)); daTarow.addVIEw(rowFIEldText); if (column.isEnabled) { rowFIEldText.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { if (cellClickListener != null) { cellClickListener.onClick(row,column.datafield); } } }); } else { rowFIEldText.setTextcolor(color.rgb(128,128,128)); } } if (rowValIDatorListener != null){ rowValIDatorListener.onValIDator(daTarow,row); } if (index == -1){ bodyRow.addVIEw(daTarow); }else { bodyRow.addVIEw(daTarow,index); } daTarow.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { if (rowClickListener != null) { rowClickListener.onClick(row); } } }); } public voID updateRow(Map<String,String> row){ int index = rows.indexOf(row); bodyRow.removeVIEwAt(index); this.insertRow(row,index); } public Map<String,String> getRow(int index) { return rows.get(index); } public int getColumnsCount() { return columns.size(); } public int getRowsCount() { return rows.size(); } public interface CellClickListener{ voID onClick(Map<String,String> rowData,String datafield); } public interface RowClickListener{ voID onClick(Map<String,String> rowData); } public interface RowValIDatorListener{ voID onValIDator(VIEwGroup v,Map<String,String> rowData); }}代码里面用到的列属性类也附上:
public class DataGrIDVIEwColumn { public String datafield; public String headerText; public int columnWIDth; public int textAlign; public int textcolor; public boolean isEnabled;}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android自定义DataGridView数据表格控件全部内容,希望文章能够帮你解决Android自定义DataGridView数据表格控件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)