0

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

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

4 回答 4

10

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

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

    @Override
    public int getItemViewType(int position) {

        return position;
    }
于 2013-09-30T10:59:03.390 回答
0

尝试如下:

查看带有 RadioButtons 的 Listview它可以帮助您解决问题。

于 2013-09-30T05:53:23.410 回答
0

您应该使用Map<Integer, <Value>>然后将选中的收音机项目放入其中。关键是列表视图的项目位置。价值; 您的单选按钮视图值。

于 2013-09-30T13:42:45.367 回答
0

在我的情况下,我的listview项目包含radiogroup3 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>();

电台组 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));
}
于 2014-05-04T17:01:18.730 回答