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

用 ListView 打造自適應表格:靈活配置每個儲存格大小

編輯:關於 Android 程式設計

若要在 Android 上實作表格,GridView 雖然比 ListView 更容易做出自適應效果,但它的缺點是每個格子的尺寸固定;反觀 ListView,它能讓每個儲存格的大小各不相同,代價是實作起來相對複雜一些。此外,GridView 的表格可以精準定位到某個儲存格,而 ListView 只能定位到「列」的層級。所以老話一句:還是得依實際使用情境,來權衡到底該用 GridView 還是 ListView。

先來看看本文程式的執行成果:

本文實作的 ListView 表格有幾個特色:每個儲存格大小可以不一,格內可放文字(TextView)或圖片(ImageView)作為資料,而且不需要預先定義 XML 樣式——這正是「自適應」的核心目標。由於 ListView 被放進 HorizontalScrollView 裡,因此面對欄位眾多、或欄位資料特別長的表格,也能從容應對其寬度。

版面配置 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <HorizontalScrollView android:id="@+id/HorizontalScrollView01"
        android:layout_height="fill_parent" android:layout_width="fill_parent">
        <ListView android:id="@+id/ListView01" android:layout_height="wrap_content"
            android:layout_width="wrap_content"></ListView>
    </HorizontalScrollView>
</LinearLayout>

主程式 testMyListView.java

package com.testMyListView;
import java.util.ArrayList;
import com.testMyListView.TableAdapter.TableCell;
import com.testMyListView.TableAdapter.TableRow;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

/**
 * @author hellogv
 */
public class testMyListView extends Activity {
    ListView lv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.setTitle("ListView自適應實現表格---hellogv");
        lv = (ListView) this.findViewById(R.id.ListView01);
        ArrayList<TableRow> table = new ArrayList<TableRow>();
        TableCell[] titles = new TableCell[5];// 每行5個單元
        int width = this.getWindowManager().getDefaultDisplay().getWidth()/titles.length;
        // 定義標題
        for (int i = 0; i < titles.length; i++) {
            titles[i] = new TableCell("標題" + String.valueOf(i),
                width + 8 * i,
                LayoutParams.FILL_PARENT,
                TableCell.STRING);
        }
        table.add(new TableRow(titles));
        // 每行的數據
        TableCell[] cells = new TableCell[5];// 每行5個單元
        for (int i = 0; i < cells.length - 1; i++) {
            cells[i] = new TableCell("No." + String.valueOf(i),
                titles[i].width,
                LayoutParams.FILL_PARENT,
                TableCell.STRING);
        }
        cells[cells.length - 1] = new TableCell(R.drawable.icon,
            titles[cells.length - 1].width,
            LayoutParams.WRAP_CONTENT,
            ...);
    }
}

程式先計算出螢幕寬度後平均分配給每一欄,再依序填入標題與資料列,最後一格則放上一張圖片來示範「同一列裡可以混用文字與圖片」。透過 TableRow 與 TableCell 這兩個自訂結構,就能把整個表格的資料組織得井井有條,再交給 ListView 逐列渲染即可。

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