實(shí)現(xiàn)XRecyclerView中radiobutton單選功能

現(xiàn)在要實(shí)現(xiàn)如下圖功能(單選,選中任何一個(gè)沒(méi)其他的全部要重置,看似checkbox,實(shí)則radiobutton,UI設(shè)計(jì)出來(lái)的,別問(wèn)我radiobutton與checkbox有什么區(qū)別,我已經(jīng)跟他們講了~~~~):


image.png

遇到的問(wèn)題:
1、java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling不能在RecyclerView計(jì)算layout或者滑動(dòng)的時(shí)候使用 notifyDataSetChanged() 方法
嘗試的方法:將notifyDataSetChanged()放到點(diǎn)擊事件回調(diào)的單獨(dú)UI線程里,但是也沒(méi)用,會(huì)導(dǎo)致錯(cuò)亂,而且有時(shí)候點(diǎn)擊會(huì)閃一下
解決方法:通過(guò)標(biāo)志位來(lái)告訴layout是否當(dāng)前還在計(jì)算計(jì)算layout
2、出現(xiàn)滑動(dòng)的時(shí)候radiobutton選中錯(cuò)亂,明明選中了一個(gè),但是在滑動(dòng)的時(shí)候卻顯示在其他上面

適配器代碼如下:

    public class GiftAdapter extends SimpleRecAdapter<GiftModel, GiftAdapter.viewHolder> {

private static int TAG_VIEW = 0;
private String TAG = "GiftAdapter";
private HashMap<Integer, Boolean> states = new HashMap<>();
private boolean isBind;
public GiftAdapter(Context context) {
    super(context);
}

@Override
public viewHolder newViewHolder(View itemView) {
    return new viewHolder(itemView);
}

@Override
public int getLayoutId() {
    return R.layout.item_gift;
}

@Override
public void setData(List<GiftModel> data) {
    super.setData(data);
    clearState();
}

@Override
public void onBindViewHolder(final viewHolder holder, final int position) {
    final GiftModel giftModel = data.get(position);
    isBind = true;
    holder.tvGiftTitle.setText((position + 1) + "." + giftModel.getTitle());
    holder.radioGift.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                clearState();
                setCheckedState(position);
                if (getRecItemClick() != null) {
                    getRecItemClick().onItemClick(position, giftModel, TAG_VIEW, holder);
                }
                if(!isBind){ //標(biāo)識(shí)位
                    notifyDataSetChanged();
                }
            }
        }
    });
            //注意setChecked與setOnCheckedChangeListener的順序,同樣會(huì)導(dǎo)致錯(cuò)亂
    holder.radioGift.setChecked(states.get(position));
    isBind = false;
}

private void clearState() {
    for (int i = 0; i < getItemCount(); i++) {
        states.put(i, false);
    }
}

private void setCheckedState(int position) {
    states.put(position, true);
}

public class viewHolder extends RecyclerView.ViewHolder {
    @BindView(R.id.tv_gift_title)
    TextView tvGiftTitle;
    @BindView(R.id.radio_gift)
    RadioButton radioGift;

    public viewHolder(View itemView) {
        super(itemView);
        KnifeKit.bind(this, itemView);
    }
}
}

布局文件:R.layout.item_gift

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll_main_nav"
android:layout_width="match_parent"
android:layout_height="@dimen/list_line_normal_40"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
    android:id="@+id/tv_gift_title"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center_vertical"
    android:maxLines="1"
    android:textColor="@color/white"
    android:ellipsize="end"
    android:text="這是禮品"/>
<RadioButton
    android:id="@+id/radio_gift"
    android:layout_width="@dimen/image_line_smaller"
    android:layout_height="@dimen/image_line_smaller"
    android:button="@drawable/custom_radio"
    android:layout_marginLeft="@dimen/margin_largger"/>
</LinearLayout>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容