現(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>