0

我想在我的 ArrayAdapter 中设置一个切换按钮的 RadioGroup。我的列表中有两个选项,所以当我选择一个 ToggleButton 打开时,我希望另一个关闭。

这是我的 ArrayAdapter 代码:

public class MyAdapter extends ArrayAdapter<String>{

    public MyAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
        super(context, textViewResourceId, objects);
    }

    @Override
    public View getDropDownView(int position, View convertView,ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    public View getCustomView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        final ViewHolder holder = new ViewHolder();
        View row = inflater.inflate(R.layout.psip_settings_callquality_listitem, parent, false);

        holder.itemTitle = (TextView) row.findViewById(R.id.txtCQViewItem);
        holder.itemDesc = (TextView) row.findViewById(R.id.txtCQViewDesc);
        holder.itemToggle = (ToggleButton) row.findViewById(R.id.togCQViewItem);
        holder.itemToggle.setTag(position);

        holder.itemToggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton togButton, boolean isChecked) {

            }

        });

        holder.itemTitle.setText(sSettingTitles.get(position));
        holder.itemDesc.setText(sSettingDesc.get(position));

        return row;
    }

    private class ViewHolder {
        TextView itemTitle;
        TextView itemDesc;
        ToggleButton itemToggle;
    }
}

XML

<TextView
    android:id="@+id/txtCQViewItem"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:textColor="@color/MiscBtnClick"
    android:layout_margin="5dp" />

<TextView
    android:id="@+id/txtCQViewDesc"
    android:layout_width="wrap_content"
    android:layout_below="@+id/txtCQViewItem"
    android:layout_height="wrap_content"
    android:textColor="@color/MiscBtnClick"
    android:layout_toLeftOf="@+id/togCQViewItem"
    android:layout_margin="5dp" />

<ToggleButton
    android:id="@+id/togCQViewItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtCQViewItem"
    android:layout_alignParentRight="true" />

任何帮助都会很棒。谢谢

4

1 回答 1

0
private int selection = -1;
ViewHolder holder = null;
if(convertView == null){
    holder = new ViewHolder()
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View row = inflater.inflate(R.layout.psip_settings_callquality_listitem, parent, false);
    holder.itemTitle = (TextView) row.findViewById(R.id.txtCQViewItem);
    holder.itemDesc = (TextView) row.findViewById(R.id.txtCQViewDesc);
    holder.itemToggle = (ToggleButton) row.findViewById(R.id.togCQViewItem);
    convertView = row;
    convertView.setTag(holder);
}else{
    holder = (ViewHolder)convertView.getTag();
}

holder.itemToggle.setTag(position);
holder.itemToggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton togButton, boolean isChecked) {
        if(isChecked && position != selection){
            selection = position;
            notifyDataSetChnaged();
        }else if(!isChecked && position == selection){
            selection = -1
        }
    }
});
holder.itemToggle.setChecked(position == selection);
holder.itemTitle.setText(sSettingTitles.get(position));
holder.itemDesc.setText(sSettingDesc.get(position));
return convertView;

您不需要每次都创建视图,因为 ListView 重新使用视图。按照我建议的代码进行操作,让我知道它是否有效。

于 2014-02-18T16:54:05.400 回答