想在 Android 上實作出類似 Excel 的試算表,一般人直覺會想到去改寫 ListView 控制項,但實際嘗試後會發現相當繁瑣。後來我靈機一動,想到 TableLayout 本身也具備無限擴展的能力,於是改用它來實作。
TableLayout 自身無法滑動,通常得搭配 ScrollView 使用來處理縱向捲動;至於橫向滑動,則交給 HorizontalScrollView。就靠這三個元件的巧妙搭配,一個能上下左右自由捲動的試算表便輕鬆成形了。
整個版面由兩大部分組成:最左側是固定的「列標籤欄」,右側則是可橫向捲動的主內容區。主內容區上方是欄標題,下方是可縱向捲動的資料格。為了讓列標籤欄與主內容區在捲動時保持同步,我特別實作了一個 LinkedScrollView——它繼承自 ScrollView,並在 onScrollChanged 中把捲動位置「傳染」給其他關聯的 ScrollView,从而实现多欄同步捲動的效果。
package com.SpreadSheet;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spreadsheet);
final int NUM_COLS_AND_ROWS = 30;
String[] cols = new String[NUM_COLS_AND_ROWS];
String[] rows = new String[NUM_COLS_AND_ROWS];
LinearLayout layout = (LinearLayout) findViewById(R.id.layout_spreadsheet);
// 建立左側欄,放列標籤
LinkedScrollView lsvLeftCol = new LinkedScrollView(this);
lsvLeftCol.setVerticalScrollBarEnabled(false);
TableLayout tlLeftCol = new TableLayout(this);
TableLayout.LayoutParams tlLeftColParams = new TableLayout.LayoutParams();
tlLeftCol.setLayoutParams(tlLeftColParams);
for (int i = -1; i < rows.length; i++) {
TableRow tr = new TableRow(this);
TextView tv = new TextView(this);
tv.setBackgroundResource(R.drawable.spreadsheetbg);
if (i >= 0) // -1 是左上角空白格
{
tv.setText(rows[i]);
}
tr.addView(tv);
tlLeftCol.addView(tr);
}
lsvLeftCol.addView(tlLeftCol);
// 加入主橫向捲動區
HorizontalScrollView hsvMainContent = new HorizontalScrollView(this);
hsvMainContent.setHorizontalScrollBarEnabled(false);
LinearLayout llMainContent = new LinearLayout(this);
llMainContent.setOrientation(LinearLayout.VERTICAL);
// 加入欄標題
TableLayout tlColHeadings = new TableLayout(this);
TableRow trHeading = new TableRow(this);
for (int i = 0; i < cols.length; i++) {
TextView tv = new TextView(this);
tv.setBackgroundResource(R.drawable.spreadsheetbg);
tv.setText(cols[i]);
trHeading.addView(tv);
}
tlColHeadings.addView(trHeading);
llMainContent.addView(tlColHeadings);
// 加入主內容
LinkedScrollView lsvMainVertical = new LinkedScrollView(this);
lsvMainVertical.setVerticalScrollBarEnabled(false);
TableLayout tlMainContent = new TableLayout(this);
for (int i = 0; i < rows.length; i++) {
TableRow tr = new TableRow(this);
for (int j = 0; j < cols.length; j++) {
TextView tv = new TextView(this);
tv.setBackgroundResource(R.drawable.spreadsheetbg);
tv.setText("test");
tr.addView(tv);
}
tlMainContent.addView(tr);
}
lsvMainVertical.addView(tlMainContent);
llMainContent.addView(lsvMainVertical);
hsvMainContent.addView(llMainContent);
layout.addView(lsvLeftCol);
layout.addView(hsvMainContent);
// 讓兩個 ScrollView 互相關聯,達到同步捲動
lsvMainVertical.others.add(lsvLeftCol);
lsvLeftCol.others.add(lsvMainVertical);
}
private class LinkedScrollView extends ScrollView {
public boolean cascadeScroll = true;
public ArrayList<LinkedScrollView> others = new ArrayList<LinkedScrollView>();
public LinkedScrollView(Context context) {
super(context);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (cascadeScroll) {
for (int i = 0; i < others.size(); i++) {
others.get(i).cascadeScroll = false;
others.get(i).scrollTo(l, t);
others.get(i).cascadeScroll = true;
}
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout_spreadsheet"></LinearLayout>
這個做法的精髓在於「同步捲動」。透過在 LinkedScrollView 中維護一份 others 列表,並在捲動回呼裡逐一呼叫對方的 scrollTo(),再以 cascadeScroll 旗標避免無限遞迴,就能讓固定的列標籤欄與可捲動的資料區天衣無縫地一起移動,營造出媲美桌面試算表的操作手感。