【问题标题】:How to handle Radio Button in ExpandableListView?如何处理 ExpandableListView 中的单选按钮?
【发布时间】:2018-10-10 21:38:42
【问题描述】:

我有一个超过或等于 2 个组的 ExpandableListView。在这些组中,很少有带有单选按钮的项目。因此,当我从一组中选择一个单选按钮时,不应选择另一组中的其他按钮。

以下代码有助于使单选按钮作为单选组工作。

@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.attribute_expanable_child_item, null);
    }

    RadioButton mRadioButton = convertView.findViewById(R.id.radio_option);
    mRadioButton.setVisibility(View.VISIBLE);
    mRadioButton.setText(mItem.getAttributeName());

    mRadioButton.setChecked(childPosition == selectedPosition);
    mRadioButton.setTag(childPosition);

    mRadioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectedPosition = (Integer) view.getTag();
            notifyDataSetChanged();
        }
    });
}

现在,如果我从一个组中选择一个项目,另一个组中的另一个项目会在同一位置被选中!我知道我应该将选定的位置保存在一个数组中,但我不知道如何。 那么,我应该如何为 ExpandableListView 中的多个组实现这一点?

【问题讨论】:

  • 请贴出适配器的构造函数。

标签: android listview radio-button expandablelistview expandablelistadapter


【解决方案1】:

我找到了解决方案,但正忙于项目,所以现在发布它,考虑到有人可以从中获得帮助!

为了在可展开的 ListView 中保持与该特定组相对应的选定位置,我必须保存该位置!所以我使用 HashMap 保存了它。

所以在 hashmap 的第一个参数中,我存储了组位置和第二个视图的 ID。并在可扩展的孩子被填充时设置它。

所以最终代码如下:

Hashmap<Integer, Integer> mChildCheckStates = new Hashmap<>();

@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    final AttributePOJO mItem = (AttributePOJO) getChild(groupPosition, childPosition);
    String mGroup = (String) getGroup(groupPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.attribute_expanable_child_item, null);
    }

    RadioButton mRadioButton = convertView.findViewById(R.id.radio_option);
    mRadioButton.setVisibility(View.VISIBLE);

    try {
        mRadioButton.setChecked(childPosition == mChildCheckStates.get(groupPosition));

    }catch (Exception e){
        e.printStackTrace();
    }
    mRadioButton.setTag(childPosition);

    mRadioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mChildCheckStates.put(groupPosition, (Integer) view.getTag());
            notifyDataSetChanged();
        }
    });

    return convertView;
}

就是这样!

【讨论】:

  • 什么是'mChildCheckStates'?
  • 您好@AbhinayMe,这是一个哈希图,我在其中存储了组位置及其相关的子位置。我已经更新了代码,请检查。如果您有任何其他问题,请询问。
猜你喜欢
  • 1970-01-01
  • 2020-07-23
  • 2020-01-29
  • 1970-01-01
  • 1970-01-01
  • 2013-08-16
  • 2018-10-29
  • 2015-10-24
  • 2012-08-28
相关资源
最近更新 更多