将RadioButton插入可扩展列表视图的组中

时间:2017-10-23 13:48:14

标签: android radio-button expandablelistview expandablelistadapter

我有一个可扩展列表视图,其中包含一些包含RadioButton的组。

Example

问题是这些RadioButton中只有一个可以同时启用,但现在我可以选择所有这些。

这是实现的适配器的代码

public class ActionPlanExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<ActionPlan> listActionPlan;
    private Map<ActionPlan,ArrayList<Contact>> mapChild;

    public ActionPlanExpandableListAdapter(Context context, ArrayList<ActionPlan> listActionPlan, Map<ActionPlan, ArrayList<Contact>> mapChild) {
        this.context = context;
        this.listActionPlan = listActionPlan;
        this.mapChild = mapChild;
    }

    ...

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        String groupTitle = context.getString(R.string.actionPlanGroupHeader) + (groupPosition+1);
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.action_plan_alarm_expandable_group, null);
        }
        TextView tvGroup = (TextView) convertView.findViewById(R.id.tvAlertName);
        tvGroup.setText(groupTitle);

        RadioButton selectGroup = (RadioButton)convertView.findViewById(R.id.rbSelectActionPlan);
        selectGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        Contact contact = getChild(groupPosition,childPosition);
        String item = contact.getName();
        convertView = LayoutInflater.from(context).inflate(R.layout.action_plan_alarm_expandable_child, null);

        TextView contactName = (TextView) convertView.findViewById(R.id.tvContactName);
        TextView contactRelationship = (TextView) convertView.findViewById(R.id.tvContactRelationship);
        TextView contactPhoneNumber = (TextView) convertView.findViewById(R.id.tvContactPhoneNumber);

        contactName.setText(contact.getName());
        contactRelationship.setText(contact.getRelationship());
        contactPhoneNumber.setText(Integer.toString(contact.getPhoneNumber1()) + ", " + Integer.toString(contact.getPhoneNumber2()) + ", " + Integer.toString(contact.getPhoneNumber3()));

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

}

2 个答案:

答案 0 :(得分:0)

也许你只有糟糕的适配器实现

中尝试类似的东西

getGroupView { ... RadioButton selectGroup =(RadioButton)convertView.findViewById(R.id.rbSelectActionPlan); selectGroup.setChecked(isExpanded); ... }

我不知道哪个属性设置检查单选按钮。 当你保持无线电btn检查时,你的模型必须具有相同的属性。或者什么时候扩展设置无线电btn检查。

答案 1 :(得分:0)

最后我解决了这个问题。首先将此变量添加到适配器。

private int selectedPosition = -1;

我修改了getGroupView。

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    String groupTitle = context.getString(R.string.actionPlanGroupHeader) + (groupPosition+1);
    if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(R.layout.action_plan_alarm_expandable_group, null);
    }
    TextView tvGroup = (TextView) convertView.findViewById(R.id.tvAlertName);
    tvGroup.setText(groupTitle);

    RadioButton selectGroup = (RadioButton)convertView.findViewById(R.id.rbSelectActionPlan);
    selectGroup.setChecked(groupPosition == selectedPosition);
    selectGroup.setTag(groupPosition);
    selectGroup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            buttonCheckChanged(view);
        }
    });

    return convertView;
}

private void buttonCheckChanged(View view) {
    selectedPosition = (Integer) view.getTag();
    notifyDataSetChanged();
}