Use ListView to draw a custom table in Android (3)

Source: Internet
Author: User

Use ListView to draw a custom table in Android (3)

The custom table is improved to support row merging. Table is divided into two modes: simple and complex

1. The simple mode is described in "using ListView in Android to draw a custom table (2. Row merging is not supported.

2. Complex Mode supports row/column merge

1. Based on the uploaded code, modify the file as follows:

 

Package csdn. danielinbiti. custometableview. item; public class ItemCell {private String cellValue =; // The value of the cell is private int cellSpan = 1; // The private CellTypeEnum cellType = CellTypeEnum across cells. LABEL; // cell type private int colNum = 0; // cell column number, starting from 0 private int rowNum = 0; // starting from 0, each item starts from 0 and private int rowSpan = 1; // The cell cross-row // private int rowType = 0; // The row type is private boolean isChange = false; // whether the public ItemCell (String cellValue, CellTypeEnum cellType, int cellSpan) is edited {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 position. The column is based on the value accumulated by the preceding column + rowspan number. each row of rownum starts from 0 public void setPos (int rowNum, int colNum, int rowSpan) {this. rowNum = rowNum; 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; // whether to read only private ArrayList
 
  
ViewList = new ArrayList (); // table list of rows private HashMap
  
   
ViewMap = new HashMap (); // key is the combination of rows and columns. private int [] headWidthArr = null; // set the column width of the header to private String rowType = 0; // The row style idprivate int rowHeight = 0; private boolean isSimple = true; // whether simple row mode (no row merging) 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: Specifies the row style. The characters are arbitrary. For rows with the same style, you do not need to create * itemCells: cell information * headWidthArr: width of each column * isRead: Read-only or not. If read-only, all inputs do not take effect */public void buildItem (Context context, String rowType, ArrayList
   
    
ItemCells, int [] headWidthArr, boolean isRead) {if (this. getTag ()! = Null & this. getTag () instanceof String & this. getTag (). equals (2) {// set to 2 to merge complex rows this. isSimple = false;} this. setOrientation (LinearLayout. VERTICAL); // the layout of the first layer is 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
    
     
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) {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) {if (this. isSimple) {this. refreshDataSimple (itemCells);} else {this. refreshDataR (itemCells);} private void refreshDataSimple (ArrayList
       
         ItemCells) {for (int I = 0; I
        
          ItemCells) {for (int I = 0; I
         
           2. You can configure the layout file in simple mode and complex mode. If this parameter is not set, the default mode is simple.
          

 

 

           
           

Android: tag = 2 is a complex mode and is not configured as a 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); this. testAddRows (rowMap1, 1, r1-1-4 (1), CellTypeEnum. DIGIT, 2); // note the ID of this column number, which must be the first column number + cross-row number rowMap1.put (rowtype, css1 ); // Add 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, 2); rowMap2.put (rowtype, css2); // Add a 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, 3); rowMap3.put (rowtype, css3); // Add a 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, 3); rowMap4.put (rowtype, css3); // Add a table sample label after adding the content}

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. Replace the original code with the following code to this. testAddContentRows (contentMap); // Complex Mode


The row/column merge mode has been implemented for the Dynamic Layout Based on the listview mode, and it has come to an end. I hope to help the following users. The settings of some styles are not provided here, including the numeric keyboard when the number is entered, the data acquisition when the input is changed, and other methods are not provided, only the users can add or subtract as needed.

 

It is recommended that you have a simple layout designer such as html to generate the data that can be used, which is relatively better, or manual data calculation is prone to errors.

 

 

 

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.