Improved the custom table to support row merging. The table is divided into simple and complex two modes
1. The simple mode is the way that Android uses the ListView to draw a custom table (2). Row Merge Not supported
2. Complex mode supports row and column merging
1, based on the last uploaded code, change the file as follows
Package Csdn.danielinbiti.custometableview.item;public class Itemcell {private String cellvalue = "";//cell Value private int CellSpan = 1; Cell Cross Column private celltypeenum celltype = Celltypeenum.label; Cell type private int colnum = 0; Unit column number, starting from 0 private int rowNum = 0;//starting from 0, each item starts with 0 private int rowSpan = 1;//cell crosses row//private int rowType = 0; Row Type Private Boolean ischange = false;//is edited public Itemcell (String cellvalue,celltypeenum celltype,int cellSpan) { This.cellvalue = Cellvalue;this.celltype = Celltype;this.cellspan = CellSpan;} Public Itemcell (String cellvalue, Celltypeenum celltype) {this (cellvalue,celltype,1);} 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 Celltypeenum Getcelltype () {return celltype;} public int Getcellspan () {return cellSpan;} public void Setischange (Boolean ischange) {this.ischange = Ischange;} public Boolean Getischange () {return this.ischange;} Set the row and column positions, the columns are incremented according to the preceding column +rowspan numbers, rowNum each line starts from 0 public void SetPos (int rownum,int colnum,int rowSpan) {this.rownum = ro Wnum; This.colnum = Colnum; This.rowspan = RowSpan; }public int Getrownum () {return rowNum;} public int Getrowspan () {return rowSpan;} public int getId () {return this.rownum * 10000 + This.rowspan; }}
Custometableitem.java
Package Csdn.danielinbiti.custometableview.item;import Java.util.arraylist;import Java.util.hashmap;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.relativelayout;import Android.widget.textview;import Android.widget.linearlayout.layoutparams;public class Custometableitem extends LinearLayout {Private context context = Null;private Boolean isread = false;//is read-only private arraylist<view> viewlist = new ArrayList ();//row Table list private hashmap<string,view> Viewmap = new HashMap ()//key for row and column combination private int[] Headwidtharr = null;//Header column width set private String RowType = "0";//Line style idprivate int rowHeight = 0;private Boolean issimple = true;//whether simple row mode (no row merge) 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: Line style, any character, the same style of line does not need to create the * itemcells: Cell information * Headwidtharr: Width per column * Isread: Whether read-only, if it is read-only, then all input is not effective */public voi D Builditem (Context context,string rowtype,arraylist<itemcell> itemcells, int[] Headwidtharr,boolean isRead) { if (This.gettag ()!=null && this.gettag () instanceof String && This.gettag (). Equals ("2")) {//set to 2 for complex row fit and this.issimple = false; }this.setorientation (linearlayout.vertical);//First level layout vertical this.context = context; This.headwidtharr =headwidtharr.clone (); This.rowtype = RowType; if (rowheight==0) {rowHeight = dp2px (context,40); } if (issimple) {This.addcell (itemcells); }else{This.addcellr (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.getcell Span ();//the number of rows int width = getcellwidth (cellindex,endindex);//Line width 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) {//Insert vertical line LinearLayout V_line = (linearlayout) getverticalline (); V_line.setlayoutparams (new Linearlayout.layoutparams (layoutparams.wrap_content,layoutparams.wrap_content)); SecondLayout.addView (v_line);} }} private void Addcellr (arraylist<itemcell> itemcells) {this.removeallviews (); Relativelayout secondlayout = new Relativelayout (context);//secondlayout.setorientation (linearlayout.horizontal); Secondlayout.setlayoutparams (New Relativelayout.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 = Itemcell.getcolnum () +itemc Ell.getcellspan ();//the number of rows int width = getcellwidth (Itemcell.getcolnum (), endIndex);//Line width int height = getcellheight ( Itemcell.getrowspan ()); int left = THIS.GEtcellleft (Itemcell.getcolnum ()); int top = This.getcelltop (Itemcell.getrownum ()); Relativelayout.layoutparams RL = new Relativelayout.layoutparams (LAYOUTPARAMS.WRAP_CONTENT,LAYOUTPARAMS.WRAP_ CONTENT); Rl.leftmargin = left; Rl.topmargin = top; if (Itemcell.getcelltype () ==celltypeenum.string) {EditText view= (EditText) Getinputview (); View.settext ( Itemcell.getcellvalue ()); View.setwidth (width); view.setheight (height); This.seteditview (view); Secondlayout.addview (VIEW,RL); Viewmap.put (Itemcell.getid () + "", view); }else if (Itemcell.getcelltype () ==celltypeenum.digit) {EditText view= (EditText) Getinputview (); View.settext ( Itemcell.getcellvalue ()); View.setwidth (width); view.setheight (height); This.seteditview (view); This.setonkeyborad (view); Secondlayout.addview (VIEW,RL); Viewmap.put (Itemcell.getid () + "", view); }else if (Itemcell.getcelltype () ==celltypeenum.label) {TextView view = (TextView) getlabelview (); View.settext ( Itemcell.getcellvalue ()); View.setwidth (width); View.setheight (height); Secondlayout.addview (VIEW,RL); Viewmap.put (Itemcell.getid () + "", view); }//if (i!=itemcells.size ()-1) {//Insert vertical bar//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) {if (This.iss imple) {this.refreshdatasimple (itemcells); }else{This.refreshdatar (itemcells); }} private void Refreshdatasimple (Arraylist<itemcell> itemcells) {for (int i=0;i<itemcells.size (); i++) {ItemC Ell 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 void Refreshdatar (Arraylist<itemcell> itemcells) {for (int i=0;i<itemcells.size (); i++) {Itemcell Itemcell = Itemcells.get (i); if (Itemcell.getcelltype () ==celltypeenum.label) {TextView view = (TextView) viewmap.get ( Itemcell.getid () + ""); View.settext (Itemcell.getcellvalue ());} else if (Itemcell.getcelltype () ==celltypeenum.digit) {EditText view= (EditText) Viewmap.get (Itemcell.getid () + "");; View.settext (Itemcell.getcellvalue ()); This.seteditview (view); This.setonkeyborad (view);} else if (Itemcell.getcelltype () ==celltypeenum.string) {EditText view= (EditText) Viewmap.get (Itemcell.getid () + "");; 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 int getcellleft (int cellindex) {//numbering int left = 0;for (int i=0;i<cellindex;i++) {//starting from 0} + Left;} return left;} private int getcelltop (int rowNum) {//rownum starts from 0 return rownum*this.rowheight;} private int dp2px (context context, float DP) {final FLOAT scale = context.getresources (). Getdisplaymetrics (). Density ; return (int) (DP * scale + 0.5f); } private int Getcellheight (int rowSpan) {return rowSpan * this.rowheight;} 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) {//numeric keypad if (!this.isread) {//non-read-only}}public String Getrowtype () {RE Turn RowType;}}
2, simple mode and complex mode can be configured through the layout file. Default is simple mode without setting
<?xml version= "1.0" encoding= "Utf-8"? ><csdn.danielinbiti.custometableview.item.custometableitem android: Id= "@+id/custome_item" xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width = "Fill_parent" android:layout_height= "wrap_content" android:layout_gravity= "center_vertical" android:tag= "1" android:gravity= "center_vertical"/>
Android:tag= "2" is a complex mode and is not configured for simple mode
3. Test data format
private void Testaddcontentrows (HashMap contentmap) {HashMap rowMap1 = new HashMap (); Lists.add (ROWMAP1); This.testaddrows (ROWMAP1, 1, "r1-1-1 (1)", celltypeenum.label,0,0,1); This.testaddrows (ROWMAP1, 1, "r1-2-1 (1)", celltypeenum.label,1,0,1); This.testaddrows (ROWMAP1, 1, "r1-1-2 (1)", celltypeenum.string,0,1,2); This.testaddrows ( ROWMAP1, 2, "R1-1-3 (2)", celltypeenum.string,0,2,2) this.testaddrows (ROWMAP1, 1, "r1-1-4 (1)", Celltypeenum.digit, 0,4,2);//Note the identification of this column number must be the preceding column number + the number of Rowmap1.put ("RowType", "Css1"),//The sample is placed after the content is added HashMap rowMap2 = new HashMap (); Lists.add (ROWMAP2); This.testaddrows (ROWMAP2, 1, "r2-1-1 (1)", celltypeenum.label,0,0,1); This.testaddrows (ROWMAP2, 1 , "r2-2-1 (1)", celltypeenum.label,1,0,1), This.testaddrows (ROWMAP2, 1, "R2-1-2 (2)", celltypeenum.string,0,1,2); This.testaddrows (ROWMAP2, 2, "r2-1-3 (1)", celltypeenum.string,0,2,1); This.testaddrows (ROWMAP2, 2, "r2-2-3 (1)", celltypeenum.string,1,2,1); This.testaddrows (ROWMAP2, 1, "r2-1-4 (1)", celltypeenum.digit,0,4,2); Rowmap2.put (" RowType ","CSS2");//The sample label is placed after adding HashMap rowMap3 = new HashMap (), Lists.add (ROWMAP3), This.testaddrows (ROWMAP3, 1, "r3-1-1 (1)", celltypeenum.label,0,0,1); This.testaddrows (ROWMAP3, 1, "r3-2-1 (1)", celltypeenum.label,1,0,1); This.testaddrows ( ROWMAP3, 1, "r3-2-1 (1)", celltypeenum.label,2,0,1) this.testaddrows (ROWMAP3, 1, "R3-1-2 (2)", Celltypeenum.string, 0,1,3); This.testaddrows (ROWMAP3, 2, "r3-1-3 (1)", celltypeenum.string,0,2,1); This.testaddrows (ROWMAP3, 2, "r3-2-3 (1) ", celltypeenum.string,1,2,1); This.testaddrows (ROWMAP3, 2," r3-2-3 (1) ", celltypeenum.string,2,2,1); This.testaddrows (ROWMAP3, 1, "r3-1-4 (1)", celltypeenum.digit,0,4,3); Rowmap3.put ("RowType", "CSS3");// The sample label is placed after adding HashMap ROWMAP4 = new HashMap (), Lists.add (ROWMAP4), This.testaddrows (ROWMAP4, 1, r4-1-1 (1), celltypeenum.label,0,0,1); This.testaddrows (ROWMAP4, 1, "r4-2-1 (1)", celltypeenum.label,1,0,1); This.testaddrows ( ROWMAP4, 1, "r4-2-1 (1)", celltypeenum.label,2,0,1) this.testaddrows (ROWMAP4, 1, "R4-1-2 (2)", Celltypeenum.string, 0,1,3); this.tEstaddrows (ROWMAP4, 2, "r4-1-3 (1)", celltypeenum.string,0,2,1); This.testaddrows (ROWMAP4, 2, "r4-2-3 (1)", celltypeenum.string,1,2,1); This.testaddrows (ROWMAP4, 2, "r4-2-3 (1)", celltypeenum.string,2,2,1); This.testaddrows ( ROWMAP4, 1, "r4-1-4 (1)", celltypeenum.digit,0,4,3), Rowmap4.put ("RowType", "CSS3");//The sample label is placed after the content is added and then added}
private void Testaddrows (HashMap rowmap,int colspan,string cellvalue,celltypeenum celltype,int rownum,int colNum,int RowSpan) { Itemcell Itemcell = new Itemcell (cellvalue,celltype,colspan); Itemcell.setpos (RowNum, Colnum, RowSpan); Rowmap.put (rowmap.size () + "", Itemcell);}
This.testaddcontent (CONTENTMAP);//Simple mode, the original code, the change is replaced with the following code can be this.testaddcontentrows (CONTENTMAP);//Complex mode
The dynamic layout based on the ListView method has already realized the row and column merging mode, the basic is also the end, hoped can provide the help to the latter person. As for some of the style settings, there is no specific provision, including numeric input when the numeric keypad, input changes in the data acquisition and other methods are not provided, these can only be used by the people themselves to add and subtract.
It is recommended that you have a layout designer such as simple HTML, so that the data generated can be used relatively well, or the manual calculation of the data is prone to error.
See the Custom table (2) for the source code, and the changes have been posted on it.
Use ListView to draw a custom table in Android (3)