Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用 ListView 打造自定義表格(三):加入列合併能力

編輯:關於 Android 程式設計

在前兩篇的基礎上,我把自定義表格又往前推了一步——現在它支援「列合併」了。整體架構拆成兩種模式:簡單模式與複雜模式,開發者可依場景自由挑選。

兩種模式的分野

簡單模式:就是《Android 中使用 ListView 繪製自定義表格(2)》裡的做法,採 LinearLayout 橫向排列,實作直觀,但不具備列合併能力。

複雜模式:改用 RelativeLayout 絕對定位,每個儲存格依「列號 + 列跨數」計算座標,因此可以同時支援跨列與跨行的合併效果。

一、資料模型:ItemCell

儲存格的資訊由 ItemCell 承載,除了文字內容與類型,還多了 rowSpan、rowNum、colNum 三個描述「位置與佔用範圍」的欄位。

package csdn.danielinbiti.custometableview.item;

public class ItemCell {
    private String cellValue = "";              // 儲存格的值
    private int cellSpan = 1;                   // 儲存格跨列數
    private CellTypeEnum cellType = CellTypeEnum.LABEL; // 儲存格類型
    private int colNum = 0;                     // 儲存格列號,從 0 開始
    private int rowNum = 0;                     // 列號,每個 item 都從 0 開始
    private int rowSpan = 1;                    // 儲存格跨行數
    private boolean isChange = false;           // 是否被編輯過

    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 String getCellValue() { return cellValue; }
    public void setCellValue(String value) { this.cellValue = value; }

    public CellTypeEnum getCellType() { return cellType; }
    public int getCellSpan() { return cellSpan; }
    public int getColNum() { return colNum; }
    public int getRowNum() { return rowNum; }
    public int getRowSpan() { return rowSpan; }

    public void setIsChange(boolean isChange) { this.isChange = isChange; }
    public boolean getIsChange() { return this.isChange; }

    /**
     * 設定列座標。
     * colNum 需依「前面各列的 colSpan + rowSpan 累加值」自行計算,
     * rowNum 則是該儲存格在當前列中的序號,每行都從 0 重新開始。
     */
    public void setPos(int rowNum, int colNum, int rowSpan) {
        this.rowNum = rowNum;
        this.colNum = colNum;
        this.rowSpan = rowSpan;
    }

    public int getId() {
        return this.rowNum * 10000 + this.rowSpan;
    }
}

二、單行渲染:CustomeTableItem

CustomeTableItem 繼承 LinearLayout,代表表格中的「一列」。它依 isSimple 旗標決定要走橫向 LinearLayout 的簡易流程,還是走 RelativeLayout 的絕對定位流程。

public void buildItem(Context context, String rowType, ArrayList<ItemCell> itemCells,
                      int[] headWidthArr, boolean isRead) {
    // tag 設為 2 時啟用複雜(可合併)模式
    if (this.getTag() != null && this.getTag() instanceof String
            && "2".equals(this.getTag())) {
        this.isSimple = false;
    }
    this.setOrientation(LinearLayout.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);
    }
}

簡單模式以橫向 LinearLayout 依序 addView,寬度由 getCellWidth(cellIndex, endIndex) 累加而來:

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.getCellSpan();
        int width = getCellWidth(cellIndex, endIndex);
        // 依 cellType 決定使用 TextView 或 EditText,再 addView 進 secondLayout
        cellIndex = endIndex;
    }
}

複雜模式則改用 RelativeLayout,透過 leftMargin / topMargin 把每個儲存格放到正確位置,高度由 rowSpan 決定:

private void addCellR(ArrayList<ItemCell> itemCells) {
    this.removeAllViews();
    RelativeLayout secondLayout = new RelativeLayout(context);
    secondLayout.setLayoutParams(new RelativeLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    this.addView(secondLayout);

    for (int i = 0; i < itemCells.size(); i++) {
        ItemCell itemCell = itemCells.get(i);
        int endIndex = itemCell.getColNum() + itemCell.getCellSpan();
        int left   = this.getCellLeft(itemCell.getColNum());
        int top    = this.getCellTop(itemCell.getRowNum());
        int width  = getCellWidth(itemCell.getColNum(), endIndex);
        int height = getCellHeight(itemCell.getRowSpan());

        RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(width, height);
        rl.leftMargin = left;
        rl.topMargin = top;
        // 依 cellType 產生 TextView 或 EditText 後套用 rl
        secondLayout.addView(view, rl);
    }
}

資料刷新同樣分流:refreshDataSimple 直接按 index 取 viewList,refreshDataR 則以 getId() 組出 key 從 viewMap 取值,並依 preRowSpan 逐列累加偏移量。

三、透過版面配置切換模式

模式不必寫死在程式碼裡,直接在 XML 的 tag 屬性上做設定即可;不設定時預設就是簡單模式。

<!--?xml version=1.0 encoding=utf-8?-->
<csdn.danielinbiti.custometableview.item.CustomeTableItem
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/custome_item"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:gravity="center_vertical"
    android:tag="1" />

android:tag="1" 為簡單模式,android:tag="2" 為複雜模式,完全不寫則等同簡單模式。

四、測試資料怎麼組

複雜模式的資料組織比簡單模式講究,關鍵在於 testAddRows 的參數順序:colSpan、cellValue、cellType、rowNum、colNum、rowSpan。特別提醒,colNum 必須是「前面各列的佔用數累加值」,算錯就會錯位。

private void testAddContentRows(HashMap<String, Object> contentMap) {
    HashMap<String, Object> 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);
    rowMap1.put("rowtype", "css1"); // 表樣標示放在內容添加之後

    // rowMap2 ~ rowMap4 依同樣規則組裝,此處省略
}

private void testAddRows(HashMap<String, Object> 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(String.valueOf(rowMap.size()), itemCell);
}

呼叫端只要把原本的簡單模式換成複雜模式即可:

// this.testAddContent(contentMap);      // 簡單模式,沿用舊程式碼
this.testAddContentRows(contentMap);     // 複雜模式

結語與建議

至此,以 ListView 為基礎的動態表格已經把列合併這塊拼圖補上,大致可以告一段落,希望對後來者有些幫助。

這裡刻意沒有觸及的還包括:樣式細節設定、數字輸入時自動彈出數字鍵盤、輸入變更後如何回寫並取回資料等。這些屬於實務面的取捨,取用的人可依自身需求增減。

最後一個真心建議:與其手工拼出這些座標與跨距數字,不如先做一個簡單的 HTML 之類的版面設計器,由工具產出可用的資料結構。人工計算實在太容易出錯了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved