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

自製可自動平分空間的 Android 宮格檢視 AdvancedGridView

編輯:關於 Android 程式設計

專案裡常會需要九宮格,而且不只九宮格——四宮格、十六宮格、4×3 宮格都可能出現。與其每次重寫,不如封裝一個能依分配到的空間自動調整每一格尺寸的元件。

設計思路

這個 AdvancedGridView 直接繼承 TableLayout,透過 TableRow 與 LayoutParams 的權重機制讓所有格子平均分配空間。若直接在設計工具上使用,記得把自動產生的那幾個 TableRow 刪掉再套用。

 import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;

/**
* AdvancedGridView
* @author RobinTang
* @time 2012-10-15
*/
public class AdvancedGridView extends TableLayout {

private int rowNum = 0; // row number
private int colNum = 0; // col number
private BaseAdapter adapter = null;
private Context context = null;

public AdvancedGridView(Context context) {
super(context);
initThis(context, null);
}

public AdvancedGridView(Context context, AttributeSet attrs) {
super(context, attrs);
initThis(context, attrs);
}

private void initThis(Context context, AttributeSet attrs) {
this.context = context;

if (this.getTag() != null) {
String atb = (String) this.getTag();
int ix = atb.indexOf(',');
if (ix > 0) {
rowNum = Integer.parseInt(atb.substring(0, ix));
colNum = Integer.parseInt(atb.substring(ix+1, atb.length()));
}
}

if (rowNum <= 0) rowNum = 3;
if (colNum <= 0) colNum = 3;

if(this.isInEditMode()){
this.removeAllViews();
for(int y = 0; y < rowNum; ++y){
TableRow row = new TableRow(context);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f));
for(int x = 0; x < colNum; ++x){
View button = new Button(context);
row.addView(button, new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f));
}
this.addView(row);
}
}
}

public BaseAdapter getAdapter() {
return adapter;
}

public void setAdapter(BaseAdapter adapter) {
if(adapter != null){
if(adapter.getCount() < this.rowNum * this.colNum){
throw new IllegalArgumentException("The view count of adapter is less than this gridview's items");
}
this.removeAllViews();
for(int y = 0; y < rowNum; ++y){
TableRow row = new TableRow(context);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f));
for(int x = 0; x < colNum; ++x){
View view = adapter.getView(y * colNum + x, this, row);
row.addView(view, new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f));
}
this.addView(row);
}
}
this.adapter = adapter;
}

public int getRowNum() {
return rowNum;
}

public void setRowNum(int rowNum) {
this.rowNum = rowNum;
}

public int getColNum() {
return colNum;
}

public void setColNum(int colNum) {
this.colNum = colNum;
}
}

在設計階段預覽效果

元件會讀取自身的 Tag 屬性並解析成「列數,行數」。想看 3×3 的樣子,把 Tag 設成 3,3 即可,如下圖。當然也可以在程式碼中呼叫 setRowNum() 與 setColNum(),但務必在設定 Adapter 之前呼叫,否則不會生效。

使用建議

這個元件最適合項目數量固定的場景,例如功能入口頁。若項目數量會動態變動且可能超過一頁,改用 GridView 或 RecyclerView 搭配 GridLayoutManager 會更合適,因為它們天生支援捲動與檢視回收。

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