最近在做項(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