如何在Recyclerview中使用Expandable ListView?

时间:2017-11-11 19:04:10

标签: android android-recyclerview expandablelistadapter

所以现在已经有一个月了,我还没有解决这个简单的问题。

目标: 我希望有一个可扩展列表,其中parent是textView,child是包含2(现在)无线电的放射组。

我已经完成了所有工作但后来我了解到Expandable ListView正在重新使用建议使用recyclerview或viewholder的对象。我尝试了视图,但同样的事情发生了: 如果我展开组1并检查无线电位置1,取消展开组1并展开组2,则检查无线电位置1。为什么会这样?

这是myClassList的代码扩展了BaseExapandableListAdapter:

public int getGroupCount() {
    return titles.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    return 1;
}

@Override
public Object getGroup(int groupPosition) {
    return titles.get(groupPosition);
}

@Override
public List<String> getChild(int groupPosition, int childPosition) {
    return colorsMap.get(titles.get(groupPosition));
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    String title = (String) this.getGroup(groupPosition);
    if(convertView == null){
        LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        assert (layoutInflater != null);
        convertView = layoutInflater.inflate(R.layout.parent_item_s, parent, false);
    }
    TextView textView = (TextView) convertView.findViewById(R.id.titleContent);
    textView.setTypeface(null, Typeface.BOLD);
    textView.setText(title);
    return convertView;
}
class ViewHolder{
    RadioButton radioButton;
    RadioButton radioButton1;

    ViewHolder(View view){
            radioButton = (RadioButton) view.findViewById(R.id.rd1);
            radioButton1 = (RadioButton) view.findViewById(R.id.rd2);
    }


}

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

    List<String> color = colorsMap.get(titles.get(groupPosition));
    ViewHolder holder = null;
    if(convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        assert (layoutInflater != null);
        convertView = layoutInflater.inflate(R.layout.child_item_s, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    }

    holder = (ViewHolder) convertView.getTag();
    holder.radioButton.setText(color.get(0));
    holder.radioButton1.setText(color.get(1));

    return convertView;
}

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

如何确保每个孩子都有自己的对象?

1 个答案:

答案 0 :(得分:0)

有些人已经遇到过您的问题,并为他们创建了解决方案。

查看afollestad's sectioned-recyclerviewdavideas's FlexibleAdapter。他们都有关于如何使用他们的图书馆的指南。