Android:当列表视图滚动时,单选按钮会不断更改其状态

时间:2013-09-30 05:36:09

标签: android radiobuttonlist radio-group

我在列表项中有一个带有三个单选按钮的列表视图,问题是当我滚动列表视图时,所选位置的单选按钮会被更改。所以即使滚动列表视图,请告诉我如何保持单选按钮的选择不变。我的代码是,

RadioGroupAdapter.java

public RadioGroupAdapter(Context context, int layoutResourceId,
            Option[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        MatrixHolder holder = null;



        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new MatrixHolder();
            holder.txtTitle = (TextView) row.findViewById(R.id.heading);
            holder.group = (RadioGroup) row.findViewById(R.id.radio_group1);
            final RadioButton[] rb = new RadioButton[2];
            for(int i=0; i<2; i++){

                rb[i]  = new RadioButton(context);
               // rb[i].setButtonDrawable(R.drawable.single_radio_chice);
                rb[i].setId(i);
                RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
                        0, LayoutParams.WRAP_CONTENT);
                params.weight=1.0f;
                params.setMargins(5, 0, 5, 10);
                holder.group.addView(rb[i],params); //the RadioButtons are added to the radioGroup instead of the layout
            }


           // ((MatrixHolder)holder).group.clearCheck();


            row.setTag(holder);
        } else {
            holder = (MatrixHolder) row.getTag();
        }

        Option option = data[position];
        holder.txtTitle.setText(option.title);
        return row;
    }

4 个答案:

答案 0 :(得分:9)

通过添加两个不同的功能解决了我的问题: 我知道它几乎是一个黑客,但它的工作原理。所以没关系;)

@Override
    public int getViewTypeCount() {                 
                  //Count=Size of ArrayList.
        return data.length;
    }

    @Override
    public int getItemViewType(int position) {

        return position;
    }

答案 1 :(得分:0)

试试如下:

结帐Listview with RadioButtons它可以帮助您解决问题。

答案 2 :(得分:0)

您应该使用Map<Integer, <Value>>然后将已检查的广播项目放入其中。关键是listview的项目位置。值;您的单选按钮视图值。

答案 3 :(得分:0)

在我的情况下,我的listview项目包含radiogroup,其中包含3 radioButtons,这就是我解决问题的方法

首先,我为我的3行声明了3张地图(包含单选按钮1,2,3)

private static Map<Integer, Boolean> firstRow = new HashMap<Integer, Boolean>();
private static Map<Integer, Boolean> secondRow = new HashMap<Integer, Boolean>();
private static Map<Integer, Boolean> thirdRow = new HashMap<Integer, Boolean>();

Radio Group onCheckedCangedListener:

group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        switch (checkedId) {
            case R.id.opt1:
                if(!firstRow.containsKey(pstn)){
                    firstRow.put(pstn, true);
                    secondRow.remove(pstn);
                    thirdRow.remove(pstn);
                }
                break;
            case R.id.opt2:
                if(!secondRow.containsKey(pstn)){
                    secondRow.put(pstn, true);
                    firstRow.remove(pstn);
                    thirdRow.remove(pstn);
                }
                break;
            case R.id.opt3:
                if(!thirdRow.containsKey(pstn)){
                    thirdRow.put(pstn, true);
                    secondRow.remove(pstn);
                    firstRow.remove(pstn);
                }
                break;
            }
        }
    });

最后,更新单选按钮选中状态:

if(firstRow.containsKey(position)){
    r1.setChecked(firstRow.get(position));
}
if(secondRow.containsKey(position)){
    r2.setChecked(secondRow.get(position));
}
if(thirdRow.containsKey(position)){
    r3.setChecked(thirdRow.get(position));
}