Reputation: 1788
I am trying to use SimpleExpandableListAdapter, I need different layouts for groups whether they are expanded or not.
There is a constructor that allows to pass two layouts for groups:
SimpleExpandableListAdapter(Context context,
List<? extends Map<String, ?>> groupData,
int expandedGroupLayout,
int collapsedGroupLayout,
String[] groupFrom,
int[] groupTo,
List<? extends List<? extends Map<String, ?>>> childData,
int childLayout,
String[] childFrom,
int[] childTo)
I pass two different layouts but only ever the collapsedGroupLayout is displayed.
Anyone got this to ever work?
Thanks
Upvotes: 4
Views: 212
Reputation: 2016
I've come up with another idea to fix this.
I'm using the following getGroupView override:
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
View v;
if (convertView != null && ((Boolean) convertView.getTag()) == isExpanded) {
v = super.getGroupView(groupPosition, isExpanded, convertView, parent);
} else {
v = super.getGroupView(groupPosition, isExpanded, null, parent);
v.setTag(isExpanded);
}
return v;
}
The idea is to force to get a new layout only if the convertView has a different layout depending on the isExpanded parameter. After the 2 views have been created (expanded and collapsed), Android will reuse the correct convertView. The biggest issue with this approach is that I'm using a tag in the view. But I think that as long as nobody uses the tag but me, it is ok.
I hope it helps.
Upvotes: 0
Reputation: 1788
I looked at the source code of SimpleExpandableListAdapter and it looks like there is a bug:
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
View v;
if (convertView == null) {
v = newGroupView(isExpanded, parent);
} else {
v = convertView;
}
bindView(v, mGroupData.get(groupPosition), mGroupFrom, mGroupTo);
return v;
}
We can see that if convertView is not null, hence if the listview reuses its cell, the "isExpanded" parameter is just never used. So if the cell is visible, and gets clicked, the convertView is reused as is and the other layout is never used.
A quick solution for me is to override getGroupView and pass null for convertView every time:
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
return super.getGroupView(groupPosition, isExpanded, null, parent);
}
A better solution would be to keep a list of group positions and their states and to not reuse convertView if the state has changed.
Upvotes: 3