The last time I wrote the "Use ListView to draw a custom table in Android," Many people leave a message code is incomplete and no data sample. But because of the project reasons, can not put all the source code. In the last two days, the time has been simplified and an example has been made.
Such as
First, the function:
1. Support Column Merging
2, consider the interface refresh optimization
3. Reserved part of the interface
4, support left and right scrolling
1. Enumeration class: Celltypeenum
Package Csdn.danielinbiti.custometableview.item;public enum Celltypeenum { STRING //character, DIGIT// number , label //label}
This class defines the style supported by the table, expands it as needed, expands the new type, and also modifies how the new type style is created in Custometableitem
2. Core code Custometableitem, which corresponds to one row of the ListView item. Class supports column merging and does not implement row merging (row merge style control will be more complex, if you need to rewrite it yourself)
RowType: This value mainly represents a style that does not go in the table, and if the merged columns are the same row, it can be reused and no more is needed.
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;//Read-only PR Ivate arraylist<view> viewlist = new ArrayList ();//Line table list Private int[] Headwidtharr = null;//Header column width set private String RowType = "0";//Line Style Idpublic 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,All inputs are not valid */public void Builditem (Context context,string rowtype,arraylist<itemcell> itemcells, int[] Headwi Dtharr,boolean isread) {this.setorientation (linearlayout.vertical);//first-level layout 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.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);} }} public void RefreshData (arraylist<itemcell> itemcells) {for (int i=0;i<itemcells.size (); i++) {Itemcell ite Mcell = 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) {//numeric keypad if (!this.isread) {//non-read-only}}public String Getrowtype () {RE Turn RowType;}}
SOURCE Click Open link
Use ListView to draw a custom table in Android (2)