Using ListView to draw a custom table in Android (2), androidlistview

Source: Internet
Author: User

Using ListView to draw a custom table in Android (2), androidlistview

After I wrote "using ListView to draw a custom table in Android" last time, many people leave a message with incomplete code and no data sample. However, for project reasons, you cannot paste all the source code. In the last two days, I simplified it and made an example.

For example


I. functions:

1. Supports column merge

2. UI refresh optimization considerations

3. Reserved APIs

4. Support left and right scrolling

1. Enumeration class: CellTypeEnum

Package csdn. danielinbiti. custometableview. item; public enum CellTypeEnum {STRING // character, DIGIT // number, LABEL // tag}
This class defines the styles supported by the table. You can expand the new types as needed. Note that you can also modify the creation method of the new types of styles in CustomeTableItem.


2. The core code CustomeTableItem corresponds to a row of items in ListView. The class supports column merging, but does not implement row merging (the row merging style control is more complicated. If you need to rewrite it yourself)

Rowtype: This value mainly indicates the style of different rows in the table. If the merged columns have the same rows, they can be reused without being created.

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 private ArrayList <View> viewList = new ArrayList (); // private int [] headWidthArr = null in the table list of rows; // set private String rowType = "0" in the header; // idpublic metmetableitem (Context context) in the row style) {super (context);} public CustomeTableItem (Context context, AttributeSet attrs) {super (context, attrs);} public Cust OmeTableItem (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);}/** rowType: row style with arbitrary characters, * itemCells: cell information * headWidthArr: width of each column * isRead: Read-only or not, all input does not take effect */public void buildItem (Context context, String rowType, ArrayList <ItemCell> itemCells, int [] headWidthArr, boolean isRead) {this. setOrientation (LinearLayout. VERTICAL); // the layout of the first layer is VERTICAL this. context = contex T; 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. addV Iew (secondLayout); int cellIndex = 0; for (int I = 0; I <itemCells. size (); I ++) {ItemCell itemCell = itemCells. get (I); int endIndex = cellIndex + itemCell. getCellSpan (); // The number of rows occupied int width = getCellWidth (cellIndex, endIndex); // The row width cellIndex = endIndex; if (itemCell. getCellType () = CellTypeEnum. STRING) {EditText view = (EditText) getInputView (); view. setText (itemCell. getCellValue (); view. setWidth (width); this. setEditVi Ew (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 (); v Iew. setText (itemCell. getCellValue (); view. setWidth (width); secondLayout. addView (view); viewList. add (view);} if (I! = ItemCells. size ()-1) {// Insert the 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 itemCell = itemCells. get (I); if (itemCell. getCellType () = CellTypeEnum. LABEL) {Tex TView 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. setEd ItView (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 V Iew 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 keyboard if (! This. isRead) {// non-read-only} public String getRowType () {return rowType ;}}


Source code click to open the link








How to Use ListView to draw a table

Yes. In ListView, each item corresponds to a row in the table, and a LinearLayout (horizontal direction) is designed. Multiple columns of TextView are designed, some border lines and other effects are added, and then getView () in the adapter is created () return linearLayout.
 
For the problem that togglebutton is used simultaneously in the android custom listview, answer

Because the view in the listview is used repeatedly, that is, when a page,
If the view object in the list you see has been created and you have not performed any special operations, these views will be repeatedly referenced in subsequent list items,
Listview is used to reuse the template view, occupying the least memory and achieving the fastest speed.
You have two ways to do this.
First, modify the listview adapter and perform special processing on the getview method (requires you to have a good understanding of the principle of listview)
Second, use scrollview, the most convenient and direct

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.