Android自定义DataGridView数据表格控件
程序员文章站
2024-04-03 12:36:28
我是一个.net程序员,但是苦于公司要求开发一个android app,没办法,只能硬着头皮上了。
由于项目里面很多地方需要用到数据显示控件(类似于.net的datagr...
我是一个.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, headertext, columnwidth, gravity.start); } public void addcolumn(string datafield, string headertext, int columnwidth, int textalign){ this.addcolumn(datafield, headertext, columnwidth, textalign, color.rgb(80, 80, 80)); } public void addcolumn(string datafield, string headertext, int columnwidth, int textalign, int textcolor){ this.addcolumn(datafield, headertext, columnwidth, textalign, textcolor, true); } public void addcolumn(string datafield, string headertext, int columnwidth, int textalign, 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,string> row, 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, layoutparams.wrap_content, 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; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。