編輯:Android 開發實例
由於系統自帶的 GridView 不夠靈活,不允許拖拉控制項,因此我結合 LinearLayout 自行封裝了一個 GridView。透過本文的閱讀,你可以學會如何自定義控制項、如何使用組合控制項、如何為組合控制項添加資料源,以及如何為自定義控制項添加屬性。
我們要實現的效果如下:這是一個標準的 800×480 大小螢幕,因此設定一頁 GridView 顯示三行五列的應用程式資料,不足五列則按需顯示。上方的 1、2 也是一個封裝控制項,用來為應用程式分頁,具體實作會在下篇文章提到,本篇先聚焦於 GridView。
核心思路是:預設把我們的組合控制項設為 VERTICAL(垂直)方向。一行放五個,因此每一行用一個 HORIZONTAL(水平)方向的線性版面配置包起來;一行結束後,把這個水平版面配置加入組合控制項中,不足五個則按需加入。
要實現這個效果,我們需要兩個輔助類別。第一個用來表示 GridView 的「行」,命名為 TableRow:
public class TableRow {
private TableCell[] cell;
public TableRow(TableCell[] cell) {
this.cell = cell;
}
public int getSize() {
return cell.length;
}
public TableCell getCellValue(int index) {
if (index >= getSize()) {
return null;
}
return cell[index];
}
public int getCellCount() {
return cell.length;
}
public int getLastCellCount() {
return lastRowCount;
}
}
第二個用來表示每一行中的「欄位」,命名為 TableCell:
static public class TableCell {
private Object value;
public TableCell(Object value) {
this.value = value;
}
public Object getValue() {
return value;
}
}
我們還需要為 GridView 提供一個對外可添加資料的方法 setAdapter,其中 AppsAdapter 是一個自定義的 BaseAdapter。而真正的重點在於 bindView,它是整個 GridView 顯示資料的核心方法:
void bindView() {
removeAllViews();
int count = adapter.getCount();
TableCell[] cell = null;
int j = 0;
LinearLayout layout;
tableRowsList = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < count; i++) {
j++;
final int position = i;
if (j > getColumnCount() || i == 0) {
cell = new TableCell[getColumnCount()];
}
final View view = adapter.getView(i, null, null);
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
unCheckPressed();
checkRowID = -1;
checkColumnID = -1;
if (onItemClickEvent != null) {
onItemClickEvent.onItemClick(position, event, view);
}
return false;
}
});
view.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (onLongPress != null) {
onLongPress.onLongPress(v);
}
return true;
}
});
cell[j - 1] = new TableCell(view);
if (j == getColumnCount()) {
lastRowCount = j;
j = 0;
HashMap<String, Object> map = new HashMap<String, Object>();
TableRow tr = new TableRow(cell);
map.put("tableRow", tr);
tableRowsList.add(map);
layout = new LinearLayout(getContext());
addLayout(layout, cell, tr.getSize(), tr);
} else if (i >= count - 1 && j > 0) {
lastRowCount = j;
HashMap<String, Object> map = new HashMap<String, Object>();
TableRow tr = new TableRow(cell);
map.put("tableRow", tr);
tableRowsList.add(map);
layout = new LinearLayout(getContext());
addLayout(layout, cell, j, tr);
}
}
}
這段邏輯的精髓在於:透過計數器 j 判斷何時該換行(j == getColumnCount()),並在迴圈尾端處理最後一行「不足一整列」的殘餘情況(i >= count - 1 && j > 0)。每個子檢視都綁定了觸控與長按監聽器,並透過 onItemClickEvent 與 onLongPress 這兩個自定義介面,把事件回拋給外部的 Activity 處理。
getColumnCount() 所對應的屬性,讓我們可以從 XML 或程式碼動態改變 GridView 每行顯示的個數。這需要在建構子中透過 TypedArray 讀取樣式屬性:
public gridViewExt(Context context, AttributeSet attrs) {
super(context, attrs);
int resouceID = -1;
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.GridViewExt);
int N = typedArray.getIndexCount();
for (int i = 0; i < N; i++) {
int attr = typedArray.getIndex(i);
switch (attr) {
case R.styleable.GridViewExt_ColumnCount:
resouceID = typedArray.getInt(
R.styleable.GridViewExt_ColumnCount, 0);
setColumnCount(resouceID);
break;
}
}
typedArray.recycle();
}
當然,你必須先在 res 目錄中建立屬性 XML 檔(declare-styleable),才能讓這個自定義屬性生效。
為了支援機頂盒遙控器的方向鍵操作,這個控制項還實作了上下左右的焦點移動與選中狀態切換。透過 checkPressed、changeImageState 與 unCheckPressed 等方法,可以確保同一時間只有一個項目處於選中(pressed)狀態,切換時自動取消上一個項目的高亮。
每一列的版面配置檔大體如下,用一個垂直的 LinearLayout 承載圖示與文字:
<LinearLayout android:orientation="vertical"
android:background="@drawable/lessbtn" android:gravity="center"
android:layout_width="fill_parent" android:id="@+id/grid_layout"
android:layout_height="fill_parent"
xmlns:android="https://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/btn_appicon"
android:layout_width="55dip" android:layout_height="55dip"></ImageView>
<TextView android:id="@+id/tv_name" android:layout_width="wrap_content"
android:textColor="#030303" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/folder" android:layout_width="wrap_content"
android:visibility="invisible" android:layout_height="wrap_content"></TextView>
</LinearLayout>
完成這一系列編寫後,你就可以在 XML 中直接宣告,或在 Java 檔中用 new 建立這個控制項,但別忘了設定每行顯示的個數。下一篇將講述如何實現手勢切屏、如何分頁顯示資料,以及如何封裝分頁控制項。
Android Spinner 載入進度條實作
在 Android 中,可以透過進度條向使用者顯示某項任務的執行進展。進度條主要分為兩種形態:進度列與旋轉式載入指示器(Spinner)。本章將聚焦於後者。 Spinne
Android 選單機制(三):PopupMenu 彈出式選單實作
選單系列的最後一篇,來看 PopupMenu。與 Options Menu 佔據標題欄、Context Menu 綁定長按不同,PopupMenu 是「錨定在某個 Vie
Android Virtual Device 建立與啟動教學
AVD 是什麼 Android Virtual Device 是 Android 模擬器的虛擬裝置設定。每個 AVD 可指定系統版本、螢幕配置與硬體參數,讓開發者模擬不同
Android MediaPlayer 教學:打造一個能播放、暫停、快轉的音樂播放器
要在 Android 上播放音訊或影片,最直接的工具就是 MediaPlayer 類別。它背後接的是系統內建的播放服務,能處理本機檔案、raw 資源、assets,也能播