在ListView里面使用RadioGroup,在滑动时发现选项有粘连,这是由于复用导致的,我在解决这个问题时查了一些资料,但是问题依然存在,后来发现是我的代码顺序有问题。最终顺序为:
1.先取消监听:
viewHolder.radioGroup.setOnCheckedChangeListener(null);
2.清空所有选项:
viewHolder.radioGroup.clearCheck();
3.筛选出已经选择的选项:
if(!TextUtils.isEmpty(list.get(arg0).getAnswer())){
String answer = list.get(arg0).getAnswer();
if(TextUtils.equals(answer,"A")){
viewHolder.radioGroup.check(R.id.radio_a);
}else if(TextUtils.equals(answer,"B")){
viewHolder.radioGroup.check(R.id.radio_b);
}else if(TextUtils.equals(answer,"C")){
viewHolder.radioGroup.check(R.id.radio_c);
}else if(TextUtils.equals(answer,"D")){
viewHolder.radioGroup.check(R.id.radio_d);
}else{
viewHolder.radioGroup.clearCheck();
}
}else{
viewHolder.radioGroup.clearCheck();
}
4.重新添加监听;
完整代码如下:
适配器代码:
class SelectGanBuDatailAdapter extends BaseAdapter {
private Activity context;
private List list ;
public SelectGanBuDatailAdapter(Activity c,List list ) {
this.context = c;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return this.list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int arg0, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub arg0 是不是postionshi
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.layout_radio_item, null);
viewHolder = new ViewHolder();
viewHolder.radioGroup = (RadioGroup) convertView.findViewById(R.id.radio_group);
viewHolder.radioA = (RadioButton) convertView.findViewById(R.id.radio_a);
viewHolder.radioB = (RadioButton) convertView.findViewById(R.id.radio_b);
viewHolder.radioC = (RadioButton) convertView.findViewById(R.id.radio_c);
viewHolder.radioD = (RadioButton) convertView.findViewById(R.id.radio_d);
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.title.setText(list.get(arg0).getTitle());
viewHolder.radioGroup.setOnCheckedChangeListener(null);
viewHolder.radioGroup.clearCheck();
if(!TextUtils.isEmpty(list.get(arg0).getAnswer())){
String answer = list.get(arg0).getAnswer();
if(TextUtils.equals(answer,"A")){
viewHolder.radioGroup.check(R.id.radio_a);
}else if(TextUtils.equals(answer,"B")){
viewHolder.radioGroup.check(R.id.radio_b);
}else if(TextUtils.equals(answer,"C")){
viewHolder.radioGroup.check(R.id.radio_c);
}else if(TextUtils.equals(answer,"D")){
viewHolder.radioGroup.check(R.id.radio_d);
}else{
viewHolder.radioGroup.clearCheck();
}
}else{
viewHolder.radioGroup.clearCheck();
}
viewHolder.radioGroup.setId(arg0);
final RadioGroup group = viewHolder.radioGroup;
viewHolder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int id) {
int currentPos = (int) radioGroup.getId();
int currentId = radioGroup.getId();
System.out.print("currentId:" + currentId);
if(radioGroup == group){
switch (id){
case R.id.radio_a:
list.get(currentPos).setAnswer("A");
break;
case R.id.radio_b:
list.get(currentPos).setAnswer("B");
break;
case R.id.radio_c:
list.get(currentPos).setAnswer("C");
break;
case R.id.radio_d:
list.get(currentPos).setAnswer("D");
break;
}
}
}
});
return convertView;
}
static class ViewHolder {
private RadioGroup radioGroup;
private RadioButton radioA;
private RadioButton radioB;
private RadioButton radioC;
private RadioButton radioD;
private TextView title;
}
}
在Activity中使用:
List titleList = new ArrayList<>();
for(int i = 0 ; i < 50; i++){
ItemBean itemBean = new ItemBean();
itemBean.setTitle("这是第" + i + "个选项");
titleList.add(itemBean);
}
SelectGanBuDatailAdapter adapter = new SelectGanBuDatailAdapter(AboutScrollActivity.this,titleList);
listview.setAdapter(adapter);
布局:
实体类:
public class ItemBean {
private String title;
private String answer;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
至此完美解决该问题,如果谁有更好的解决方法,可以恢复哦。