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

Ash*_*til 0 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;
    }
Run Code Online (Sandbox Code Playgroud)

Ash*_*til 9

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

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

    @Override
    public int getItemViewType(int position) {

        return position;
    }
Run Code Online (Sandbox Code Playgroud)