【问题标题】:Insert RadioButton in a group of an Expandable List View在一组可展开列表视图中插入 RadioButton
【发布时间】:2017-10-23 13:48:14
【问题描述】:

我有一个可扩展列表视图,其中包含一些包含 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;
    }

}

【问题讨论】:

    标签: android radio-button expandablelistview expandablelistadapter


    【解决方案1】:

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

    中尝试类似的东西

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

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

    【讨论】:

      【解决方案2】:

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

      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();
      }
      

      【讨论】:

      • 使用参数 isExpanded 当组选择参数为真时,您不需要设置标签和检查位置
      • 使用 isExpanded 我将展开操作与 RadioButton 相关联,但如果按下 RadioButton,我仍然可以选择多个选项。可能我的实现不是最好的,但是有了它,我可以选择我想要的选项,而不管展开的元素是什么,并且我消除了一次选择多个 RadioButton 的可能性。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多