我正在尝试实现一个 N 级 ExpandableListView 并且我将这些代码用于getChildView
and getGroupView
:
@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
值始终为 0,getGroupView
并且始终引用 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;
}