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

Android 組件式開發(2):打造網格布局的 RadioButton 矩陣

編輯:關於 Android 開發

前言

在 Android 中,我們通常透過 RadioGroup 管理一組 RadioButton,以達到單選互斥的效果。但有些需求需要 N 行 N 列的 RadioButton 矩陣,而 RadioGroup 本質上是一個「耿直」的 LinearLayout,無法完成網格布局。本文就來分享一種以 GridView 實現網格化單選按鈕的思路。

思路

提到網格布局,最簡單的做法就是使用系統的 GridView,把 RadioButton 放進去,再自行實作按鈕之間的互斥邏輯。

1. 自訂 RadioButton

我們建立一個名為 IconRadioButton 的自訂元件:

public class IconRadioButton extends LinearLayout {
    private boolean mIsCheck;
    private ImageView mRadio;
    private TextView mTextView;
    private ColorStateList mTextColor;

    public IconRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View mContentView = LayoutInflater.from(context)
                .inflate(R.layout.btn_icon_radio, this);
        mRadio = (ImageView) mContentView.findViewById(R.id.radio);
        mTextView = (TextView) mContentView.findViewById(R.id.text);
    }

    public void setChecked(boolean isChecked) {
        mIsCheck = isChecked;
        changeUIByChecked(isChecked);
    }

    public boolean isChecked() { return mIsCheck; }

    private void changeUIByChecked(boolean isChecked) {
        if (mRadio.getVisibility() != GONE) {
            mRadio.setImageResource(isChecked
                    ? R.drawable.radio_selected : R.drawable.radio_unselected);
        }
        mTextView.setEnabled(isChecked);
    }

    public void setTextColor(ColorStateList color) {
        mTextColor = color;
        mTextView.setTextColor(color);
    }
    // 省略 setText / hiddenRadio 等方法
}

這個元件本質上就是一個 LinearLayout,內部封裝了依據是否選中來切換文字與按鈕樣式的邏輯,布局中放了一個 ImageView 與一個 TextView。

2. 定義資料模型

public class RadioItemModel {
    public String text;          // RadioButton 的文字
    public boolean isChecked;    // 是否選中
    public boolean hiddenRadio;  // 是否隱藏 radio
    public ColorStateList textColor; // 不同狀態下的文字顏色

    public RadioItemModel(String text) {
        this.isChecked = false;
        this.hiddenRadio = false;
        this.textColor = null;
        this.text = text;
    }
}

其中 ColorStateList 用來控制 TextView 在不同狀態(如 pressed)下的文字顏色。

3. 定義 Adapter

互斥邏輯在 Adapter 中實現,重點方法包括:

public class GridRadioAdapter extends BaseAdapter {
    private List<RadioItemModel> mList;
    private OnItemCheckedListener mListener;

    // 檢查是否有預設選中,若無則預設選中第一個
    private void checkDefaultChecked() {
        for (RadioItemModel item : mList) {
            if (item.isChecked) return;
        }
        mList.get(0).isChecked = true;
    }

    // 檢查互斥:若多個被選中,只保留最後一個
    private void checkMutex() {
        int checkCount = 0;
        for (RadioItemModel item : mList) {
            if (item.isChecked) checkCount++;
        }
        if (checkCount >= 2) {
            setOtherUnChecked(checkCount - 1);
        }
    }
    // getView 中為每個 IconRadioButton 設定點擊事件,
    // 點擊時呼叫 setOtherUnChecked(position) 並回呼 mListener
}

在 constructor 中會呼叫 check 來執行預設選中檢查與互斥檢查:若資料中沒有預設選中項,則讓第一個按鈕默認選中;若同時有多個被選中,則保留最後一個為選中狀態。此外 OnItemCheckedListener 為按鈕被選中時的回呼。

4. 使用方式

使用起來相當直覺:

private void initData() {
    mItemList = new ArrayList<>();
    ColorStateList csl = getResources().getColorStateList(R.color.ddt_color_tab_text);
    mItemList.add(new RadioItemModel("全部", false, true, csl));
    mItemList.add(new RadioItemModel("早餐前", false, true, csl));
    mItemList.add(new RadioItemModel("午餐前", false, true, csl));
    // ...其餘項目
    mAdapter.notifyDataSetChanged();
}

PS:若把 hiddenRadio 設為 true,還能做出如下圖的切換效果,因此它也很適合拿來做切換 Fragment 的選項卡。

以上就是以 GridView 打造網格式 RadioButton 矩陣的完整思路,希望對你有所啟發。

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