如何使用单个适配器实现多级可扩展列表视图?

时间:2017-03-06 14:29:20

标签: java android expandablelistview expandablelistadapter

这是我的可扩展列表适配器调用

TableOfContentsAdapter adapter = new    TableOfContentsAdapter(TableOfContentsActivity.this,toCList);
mExpandableListViewTOC.setAdapter(adapter);

这是我的适配器实现

class TableOfContentsAdapter extends BaseExpandableListAdapter {

private Context context;
private List<TOCLink> tocLinks;

public TableOfContentsAdapter(Context context, List<TOCLink> tocLinks) {
    this.context = context;
    this.tocLinks = tocLinks;
}

@Override
public int getChildrenCount(int groupPosition) {
    return tocLinks.get(groupPosition).getTocLinks().size();
}

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

@Override
public int getGroupCount() {
    return tocLinks.size();
}

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

@Override
public Object getChild(int groupPosition, int childPosition) {
    List<TOCLink> childTocLinks = tocLinks.get(groupPosition).getTocLinks();
    return childTocLinks.get(childPosition);
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean b, View convertView, ViewGroup viewGroup) {
    TOCLink tocLink = (TOCLink) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_header, null);
    }

    TextView groupHeader = (TextView) convertView.findViewById(R.id.header);
    groupHeader.setText(tocLink.getSectionTitle());

    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean b, View convertView, ViewGroup viewGroup) {
    TOCLink tocLink = (TOCLink) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_item, null);
    }

    TextView childItem = (TextView) convertView.findViewById(R.id.item);
    childItem.setText(tocLink.getSectionTitle());

    /*TOCLink tocLink1 = tocLinks.get(groupPosition).getTocLinks().get(childPosition);
    if (tocLink1.getTocLinks().size() > 0) {
        new TableOfContentsAdapter(context, tocLink1.getTocLinks());
    }*/
    return convertView;
}

}

我想用单个适配器实现多级可扩展列表视图。这里的问题是我能够进入1级但不能进一步。

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)