Android RadioButton多行多列實(shí)現(xiàn)

最近在做項(xiàng)目的時候卤唉,設(shè)計(jì)小哥給到了如下需求:


image.png

看上去簡單,xml寫起來:

<RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <RadioButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="xxx" />

                <RadioButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="xxx" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <RadioButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="xxx" />

                <RadioButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="xxx" />
            </LinearLayout>
        </RadioGroup>

然而悲傷的事情來了仁期,RadioGroup失去了它的單選功能桑驱,有且只有同一行的RadioButton可以單選。這是怎么回事呢跛蛋?看看RadioGroup源碼:

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        if (child instanceof RadioButton) {
            final RadioButton button = (RadioButton) child;
            if (button.isChecked()) {
                mProtectFromCheckedChange = true;
                if (mCheckedId != -1) {
                    setCheckedStateForView(mCheckedId, false);
                }
                mProtectFromCheckedChange = false;
                setCheckedId(button.getId());
            }
        }

        super.addView(child, index, params);
    }

這里僅僅判斷了RadioButton熬的!這限制就大了。

當(dāng)然解決方案也有多種:
1.重寫RadioGroup赊级,在子View的操作上做文章押框。
2.布局不變,動態(tài)的判斷點(diǎn)擊的RadioButton來清除其它的RadioGroup理逊。
3.使用RecyclerView來刷新整個列表

我使用到的方法就是第三種橡伞,原因在于RecyclerView通過GridLayoutManager可以動態(tài)的變換整個列表的列數(shù)(非常實(shí)用),而且不用去寫RadioGroup里面的布局結(jié)構(gòu)晋被,只需要根據(jù)傳入的List即可輕松實(shí)現(xiàn)兑徘。
話不多說,快看看代碼吧
布局:

<RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/radio_rv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
</RadioGroup>

是不是很簡單啊

RecyclerView rv = UiUtils.find(view, R.id.radio_rv);
RadioRecycleAdapter radioAdapter = new RadioRecycleAdapter(mApplication, valueList);
radioAdapter.setOnItemClickListener((holder, pos) -> {
    //TODO
});
rv.setLayoutManager(new GridLayoutManager(mApplication, 2));
rv.setAdapter(radioAdapter);
rv.addItemDecoration(new SpaceItemDecoration(49));
public class RadioRecycleAdapter extends RecyclerView.Adapter<RadioRecycleAdapter.MyViewHolder> {

    private Context mContext;
    private List<String> mDatas;
    private int mSelectedItem = -1;

    public interface OnItemClickListener {
        void onItemClick(String holder, int pos);
    }

    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public RadioRecycleAdapter(Context mContext, List<String> datas) {
        this.mContext = mContext;
        this.mDatas = datas;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_radio, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.radioButton.setText(mDatas.get(position));
        holder.radioButton.setChecked(position == mSelectedItem);
        holder.radioButton.setTag(position);
        holder.radioButton.setOnClickListener(v -> {
            mSelectedItem = (int) v.getTag();
            notifyItemRangeChanged(0, mDatas.size());
            if (mListener != null) {
                mListener.onItemClick(mDatas.get(holder.getLayoutPosition()), holder.getLayoutPosition());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public void addData(String data, int pos) {
        mDatas.add(pos, data);
        notifyItemInserted(pos);
    }

    public void removeData(int pos) {
        mDatas.remove(pos);

        notifyDataSetChanged();
        notifyItemRemoved(pos);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        RadioButton radioButton;

        public MyViewHolder(View itemView) {
            super(itemView);

            radioButton = (RadioButton) itemView.findViewById(R.id.radio_rb);
        }
    }
}

效果可以看最上面的圖片羡洛。Over

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末挂脑,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子欲侮,更是在濱河造成了極大的恐慌崭闲,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件威蕉,死亡現(xiàn)場離奇詭異刁俭,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)忘伞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進(jìn)店門薄翅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人氓奈,你說我怎么就攤上這事《μ欤” “怎么了舀奶?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長斋射。 經(jīng)常有香客問我育勺,道長但荤,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任涧至,我火速辦了婚禮腹躁,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘南蓬。我一直安慰自己纺非,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布赘方。 她就那樣靜靜地躺著烧颖,像睡著了一般。 火紅的嫁衣襯著肌膚如雪窄陡。 梳的紋絲不亂的頭發(fā)上炕淮,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天,我揣著相機(jī)與錄音跳夭,去河邊找鬼涂圆。 笑死,一個胖子當(dāng)著我的面吹牛币叹,可吹牛的內(nèi)容都是我干的润歉。 我是一名探鬼主播,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼套硼,長吁一口氣:“原來是場噩夢啊……” “哼卡辰!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起邪意,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤九妈,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后雾鬼,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體萌朱,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年策菜,在試婚紗的時候發(fā)現(xiàn)自己被綠了晶疼。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡又憨,死狀恐怖翠霍,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蠢莺,我是刑警寧澤寒匙,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布,位于F島的核電站躏将,受9級特大地震影響锄弱,放射性物質(zhì)發(fā)生泄漏考蕾。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一会宪、第九天 我趴在偏房一處隱蔽的房頂上張望肖卧。 院中可真熱鬧,春花似錦掸鹅、人聲如沸塞帐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽壁榕。三九已至,卻和暖如春赎瞎,著一層夾襖步出監(jiān)牢的瞬間牌里,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工务甥, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留牡辽,地道東北人。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓敞临,卻偏偏與公主長得像态辛,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子挺尿,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評論 2 355

推薦閱讀更多精彩內(nèi)容