Skip to content

Commit b49e4d6

Browse files
committed
Fixed group and child view caching in SimpleExpandableListAdapter.
Now the adapter reports the correct type count and type for group and child views by overriding the respective methods from the base class. Each group view has two types, one for expanded views, one for collapsed views. Each child view has two types, one for the last view within a group, one for the other views within a group. Change-Id: I117b2c0f7e98fb7fe2fdd35c15f7d1f9dc06674f Signed-off-by: Georg Hofmann <georg.hofmann@gmail.com>
1 parent 42c6d16 commit b49e4d6

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

core/java/android/widget/SimpleExpandableListAdapter.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
*/
3939
public class SimpleExpandableListAdapter extends BaseExpandableListAdapter {
4040
private List<? extends Map<String, ?>> mGroupData;
41+
// Keeps track of if a group is currently expanded or not
42+
private boolean[] mIsGroupExpanded;
4143
private int mExpandedGroupLayout;
4244
private int mCollapsedGroupLayout;
4345
private String[] mGroupFrom;
@@ -196,6 +198,8 @@ public SimpleExpandableListAdapter(Context context,
196198
int childLayout, int lastChildLayout, String[] childFrom,
197199
int[] childTo) {
198200
mGroupData = groupData;
201+
// Initially all groups are not expanded
202+
mIsGroupExpanded = new boolean[groupData.size()];
199203
mExpandedGroupLayout = expandedGroupLayout;
200204
mCollapsedGroupLayout = collapsedGroupLayout;
201205
mGroupFrom = groupFrom;
@@ -298,4 +302,52 @@ public boolean hasStableIds() {
298302
return true;
299303
}
300304

305+
/**
306+
* {@inheritDoc}
307+
* @return 1 for the last child in a group, 0 for the other children.
308+
*/
309+
@Override
310+
public int getChildType(int groupPosition, int childPosition) {
311+
final int childrenInGroup = getChildrenCount(groupPosition);
312+
return childPosition == childrenInGroup - 1 ? 1 : 0;
313+
}
314+
315+
/**
316+
* {@inheritDoc}
317+
* @return 2, one type for the last child in a group, one for the other children.
318+
*/
319+
@Override
320+
public int getChildTypeCount() {
321+
return 2;
322+
}
323+
324+
/**
325+
* {@inheritDoc}
326+
* @return 1 for an expanded group view, 0 for a collapsed one.
327+
*/
328+
@Override
329+
public int getGroupType(int groupPosition) {
330+
return mIsGroupExpanded[groupPosition] ? 1 : 0;
331+
}
332+
333+
/**
334+
* {@inheritDoc}
335+
* @return 2, one for a collapsed group view, one for an expanded one.
336+
*/
337+
@Override
338+
public int getGroupTypeCount() {
339+
return 2;
340+
}
341+
342+
/** {@inheritDoc} */
343+
@Override
344+
public void onGroupCollapsed(int groupPosition) {
345+
mIsGroupExpanded[groupPosition] = false;
346+
}
347+
348+
/** {@inheritDoc} */
349+
@Override
350+
public void onGroupExpanded(int groupPosition) {
351+
mIsGroupExpanded[groupPosition] = true;
352+
}
301353
}

0 commit comments

Comments
 (0)