问题描述
在ListView的Adapter中重写getView方法;为每个item中的RadioGroup设置监听,我的项目中是每个条目中有3个RadioButton。当选中某个条目的RadioButton后上下拉动LisetView发现没有设置选中的条目会模仿前面的条目导致整个界面选中混乱不堪。
示列问题根源
原因是因为listview的复用机制导致的监听器混乱
解决方式
RadioButton选中前先取消掉监听器
上代码
javaBean
public class DealSettingBean {
public int wheel; //轮
public int cardCount; //牌数
public int checked; //用来记录选中的第几个按钮
}
ListView的数据集合
//模拟数据
ArrayList<DealSettingBean> mArrayList = new ArrayList();
for (int i = 0; i <20; i++) {
DealSettingBean mDealSettingBean = new DealSettingBean();
mDealSettingBean.wheel=(i+1);
mDealSettingBean.cardCount=1;
mDealSettingBean.checked =3;
mArrayList.add(mDealSettingBean);
}
DealSettingAdapter mDealSettingAdapter = new DealSettingAdapter(this,mArrayList);
mListview.setAdapter(mDealSettingAdapter);
适配器设置
public class DealSettingAdapter extends BaseAdapter {
private static final String TAG = "DealSettingAdapter";
private Context mContext;
private ArrayList<DealSettingBean> mData;
public DealSettingAdapter(Context mcontext, ArrayList<DealSettingBean> mArrayList) {
this.mContext = mcontext;
this.mData = mArrayList;
}
@Override
public int getCount() {
return mData == null ? 0 : mData.size();
}
@Override
public DealSettingBean getItem(int position) {
return mData == null ? null : mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_deal_setting, null);
holder = new ViewHolder();
holder.mTv = (TextView) convertView.findViewById(R.id.item_deal_setting_tv);
holder.mRadioGroup = (RadioGroup) convertView.findViewById(R.id
.item_deal_setting_radiogroup);
holder.mRadioButton1 = (RadioButton) convertView.findViewById(R.id
.item_deal_setting_radioBtn1);
holder.mRadioButton2 = (RadioButton) convertView.findViewById(R.id
.item_deal_setting_radioBtn2);
holder.mRadioButton3 = (RadioButton) convertView.findViewById(R.id
.item_deal_setting_radioBtn3);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
DealSettingBean itemBean = mData.get(position);
holder.mTv.setText(itemBean.wheel + "");
//取消监听器,防止RadioGroup显示混乱 关键代码
holder.mRadioGroup.setOnCheckedChangeListener(null);
switch (itemBean.checked) {
case 1:
holder.mRadioGroup.check(R.id.item_deal_setting_radioBtn1);
break;
case 2:
holder.mRadioGroup.check(R.id.item_deal_setting_radioBtn2);
break;
case 3:
holder.mRadioGroup.check(R.id.item_deal_setting_radioBtn3);
break;
default:
holder.mRadioGroup.clearCheck();
break;
}
holder.mRadioGroup.setId(position);
holder.mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
DealSettingBean bean = mData.get(group.getId());
switch (checkedId) {
case R.id.item_deal_setting_radioBtn1:
bean.checked = 1;
break;
case R.id.item_deal_setting_radioBtn2:
bean.checked = 2;
break;
case R.id.item_deal_setting_radioBtn3:
bean.checked = 3;
break;
}
}
});
return convertView;
}
class ViewHolder {
TextView mTv;
RadioGroup mRadioGroup;
RadioButton mRadioButton1;
RadioButton mRadioButton2;
RadioButton mRadioButton3;
}
}
网友评论