N级ExpandableListView

时间:2013-07-04 08:41:46

标签: android expandablelistview android-adapter

我正在尝试实现N级ExpandableListView,我将这些代码用于getChildViewgetGroupView

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

    ExpandNode child = (ExpandNode) getChild(groupPosition, childPosition);
    ExpandNode group = (ExpandNode) getGroup(groupPosition);

    if (group.hasChild()) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.index_expandable_list, null);
        }
        ExpandableListView expandableListView = (ExpandableListView) view.findViewById(R.id.myExpandableListView);
        ExpandAdapter adapter = new ExpandAdapter(context, group.getChilds());
        expandableListView.setAdapter(adapter);
    } else {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.index_child_row, null);
        }
        TextView childTextView = (TextView) view.findViewById(R.id.childTextView);
        childTextView.setText(child.getTitle().toString());
    }
    return view;
}

和:

@Override
public View getGroupView(int groupPosition, boolean isLastChild, View view,ViewGroup parent) {
    ExpandNode group = (ExpandNode) getGroup(groupPosition);
    if (view == null) {
        view = LayoutInflater.from(context).inflate(R.layout.index_parent_row, null);
    }
    TextView childTextView = (TextView) view.findViewById(R.id.parentTextView);
    childTextView.setText(group.getTitle().toString());
    return view;
}

问题是当我使用嵌套适配器时,就像你在getChildView中看到的那样,groupPosition值在内部适配器的getGroupView中始终为0,并始终引用第一个arrayList中的值。 例如: arrayList中的值是:

Level1
    level1_1
    level1_2
    level1_3
level2

但它显示:

Level1
    level1_1
    level1_1
    level1_1
level2

只有当我使用嵌套适配器时才会发生这种情况,并且在使用getChildView这样的情况下它会起作用:

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

    ExpandNode child = (ExpandNode) getChild(groupPosition, childPosition);
    if (view == null) {
        view = LayoutInflater.from(context).inflate(R.layout.index_child_row, null);
    }
    TextView childTextView = (TextView) view.findViewById(R.id.childTextView);
    childTextView.setText(child.getTitle().toString());
    return view;
}

但它不再是N级列表...... 有什么建议我该如何解决这个问题?

我在下面提到的适配器中实现的其他方法:

    @Override
public Object getChild(int groupPosition, int childPosition) {
    return groups.get(groupPosition).getChilds().get(childPosition);
}

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

@Override
public int getChildrenCount(int groupPosition) {
    return groups.get(groupPosition).getChilds().size();
}

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

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

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

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

@Override
public boolean isChildSelectable(int arg0, int arg1) {
    return true;
}

1 个答案:

答案 0 :(得分:0)

当我遇到问题时。它位于getChildCount(int),其中包括另一个ExpandableListView的所有孩子的计数。我还在努力。会很快发布回复。将这个问题标记为明星,所以不会忘记回答