Android uses ListView to draw custom table tips to share _android

Source: Internet
Author: User
Tags stub
Let's take a look at the effect you can achieve.

There are several ways to achieve the effect
1, column is not fixed: You can generate different number of columns depending on the data source
2. Table contents can merge columns according to the definition of data source
3, to fill in the cell can choose a custom keyboard or system keyboard
Running these three points, made a simple implementation, put the source code (because this point is part of the main interface, not easy to put the entire demo)
Custom adapters, Callbackinterface is a custom callback interface, where the callback is defined because the data input needs to be saved in time
Copy Code code as follows:

public class Sitedetailviewadapter extends Baseadapter implements callbackinterface{
private context;
Private Layoutinflater Inflater;
Private arraylistPrivate keyboard keyboard = null;//Custom keyboard
Private ListView ListView = null;
Private Boolean isreadonly = false;//is browse state
Private string[] Arrcelltype = null;
Private int[] Arrheadwidth = null;//width per column

Public Sitedetailviewadapter, arraylist, 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);
}
Need to release annotation after performance tuning 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, 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 the layout of each line, the internal code is redundant, because it is a demo, so the effect is not accurate, no code refactoring, attention can not go to the listitemcustom in each row of the map to be worth getting or input updates, etc., because map is by address way, Combined with the way ListView is plotted, the final map is not the map you imagined.
Copy Code code as follows:

public class Listitemcustom extends linearlayout{
Public Listitemcustom {
Super (context);
}
Public Listitemcustom (context context, AttributeSet Attrs) {
Super (context, attrs);
}
Private String type = "-1";
private 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
, ListView listview,keyboard keyboard,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.getcellkey ());
This.setonkeyborad (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. Inflate (R.layout.atom_line_v_view, NULL);
}
Private View Getreadview () {
Return (View) layoutinflater.from. Inflate (R.layout.atom_text_view, NULL);
}
Private View Getinputview () {
Return (View) layoutinflater.from. Inflate (R.layout.atom_edttxt_view, NULL);
}
private void Setonkeyborad (EditText edttext1,int index) {
if (!this.isread) {
Onlistentext (Edttext1,this.listview,index);
}
}
private void Onlistentext (EditText edttext,listview listview,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-A-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 arg1, int arg2,
int Arg3) {

}
});
}
}
Public String GetType () {
return this.type;
}
}

Copy Code code as follows:

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,string cellkey,long celltype,int cellinrow,int cellspan) {
This.cellvalue = Cellvalue;
This.celltype = Celltype;
This.cellspan = Cellspan;
This.cellkey = Cellkey;
This.cellinrow = Cellinrow;
}
Public Itemcell (String cellvalue,string cellkey,long celltype,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 file
Copy Code code as follows:

<?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>

The above is the core document content. With the column merge, the row merge is not far off. You can add a linearlayout to a custom layout to implement a row merge.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.