返回 登录
0

ListView嵌套RadioButton导致的多选

1、首先ListView获取点击事件需要设置RadioButton的焦点android:focusable=”false”
2、通过一个List集合管理所有的RadioButton,然后在点击事件中,清空所有的状态。

private List<RadioButton> raList;
raList = new ArrayList<RadioButton>();
listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int item,
            long arg3) {
        clearCheck();
        RadioButton radioButton = (RadioButton) view.findViewById(R.id.radio_check);
        radioButton.setChecked(true);
    }
});
/**
 * 清空所有选中状态
 */
private void clearCheck(){
    for(RadioButton radioButton : raList){
        radioButton.setChecked(false);
    }
}

public View getView(int position, View convertView, ViewGroup parent) {
    convertView = inflater.inflate(R.layout.layout_alertdialog_item, null);
    TextView tv_content = (TextView) convertView.findViewById(R.id.context);
    tv_content.setText(listContent.get(position));
    RadioButton radioButton = (RadioButton) convertView.findViewById(R.id.radio_check);
    raList.add(radioButton);
    return convertView;
}

同时需要注意,此时要把RadioButton设置为不可点击。android:clickable=”false”

评论