I'm trying to implement a N-level ExpandableListView and I'm using these codes for 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;
}
and :
@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;
}
The problem is when I'm using nested adapters like what you see in getChildView
, the groupPosition
value is always 0 in getGroupView
in the inner adapter and always referring to the first value in the arrayList.
for example:
the values in the arrayList are:
Level1
level1_1
level1_2
level1_3
level2
but it shows:
Level1
level1_1
level1_1
level1_1
level2
It happens only when I'm using nested adapters and it works when using getChildView
like this:
@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;
}
But it's not a N-level list anymore... Is there ay suggestion how can I fix this?
Other implemented methods in my Adapter mentioned below :
@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;
}