自定义列表视图查看未保持RadioGroup状态的持有者模式

时间:2016-01-13 21:29:42

标签: java android

我已经使用RadioGroup实现了CustomListView。当我选择第一行/开始行的单选按钮并滚动到列表末尾时,将自动选择最后一行元素。当我滚动回List的开头时,Radiogroup状态不会被维护。我花了很多时间来解决这个问题。 请帮助我解决我犯错误的问题。

MyAdapter.java

public class MyAdapter extends ArrayAdapter<Model> {

private final List<Model> list;
private final Activity context;

public MyAdapter(Activity context, List<Model> list) {
    super(context, R.layout.row, list);
    this.context = context;
    this.list = list;
}

static class ViewHolder {
    protected TextView text;
    protected RadioGroup radioGroup;
    protected RadioButton firstRadio;
    protected RadioButton secondRadio;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        convertView = inflator.inflate(R.layout.row, null);
        viewHolder = new ViewHolder();
        viewHolder.text = (TextView) convertView.findViewById(R.id.label);
        viewHolder.radioGroup = (RadioGroup) convertView.findViewById(R.id.radio_group);
        viewHolder.firstRadio = (RadioButton) convertView.findViewById(R.id.first_rad);
        viewHolder.secondRadio = (RadioButton) convertView.findViewById(R.id.second_rad);
        viewHolder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int selectedID) {
                int getPosition = (Integer) radioGroup.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                list.get(getPosition).setSelected(selectedID); // Set the value of checkbox to maintain its state.
            }
        });

        convertView.setTag(viewHolder);
        convertView.setTag(R.id.label, viewHolder.text);
        convertView.setTag(R.id.radio_group, viewHolder.radioGroup);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.radioGroup.setTag(position); // This line is important.

    viewHolder.text.setText(list.get(position).getName());
    if(list.get(position).getSelected() == viewHolder.firstRadio.getId()){
        viewHolder.firstRadio.setChecked(true);
    }else if(list.get(position).getSelected() == viewHolder.secondRadio.getId()){
        viewHolder.secondRadio.setChecked(true);
    }

    return convertView;
}
}

row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:textSize="30sp" >
</TextView>

<RadioGroup
    android:id="@+id/radio_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/label"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/first_rad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="4dip"
        android:layout_marginRight="10dip"
        android:focusable="false"
        android:focusableInTouchMode="false" >
    </RadioButton>

    <RadioButton
        android:id="@+id/second_rad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="4dip"
        android:layout_marginRight="10dip"
        android:focusable="false"
        android:focusableInTouchMode="false" >
    </RadioButton>
</RadioGroup>

Model.java

public class Model {

private String name;
private int selected;

public Model(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public int getSelected() {
    return selected;
}

public void setSelected(int selected) {
    this.selected = selected;
}
} 

2 个答案:

答案 0 :(得分:1)

更改if statement中的条件,如下所示,它将起作用。

if(list.get(position).getSelected() == viewHolder.firstRadio.getId())
{
    viewHolder.firstRadio.setChecked(true);
    viewHolder.secondRadio.setChecked(false);
}
else if(list.get(position).getSelected() == viewHolder.secondRadio.getId())
{
    viewHolder.secondRadio.setChecked(true);
    viewHolder.firstRadio.setChecked(false);
}
else
{
    viewHolder.secondRadio.setChecked(false);
    viewHolder.firstRadio.setChecked(false);
}

答案 1 :(得分:0)

我不确定它现在是如何工作的。我在这个问题上花了很多天。覆盖getViewTypeCount和getItemViewType解决了我的问题。

@Override
public int getViewTypeCount() {
    if (objects.size() > 0) {
        return objects.size();
    }
    return 1;
}

@Override
public int getItemViewType(int position) {
    return position;
}