Skip to content

Commit 6a8186f

Browse files
Jean-Baptiste Queruandroid code review
authored andcommitted
Merge "Fixed group and child view caching in SimpleExpandableListAdapter."
2 parents cbc71bd + b49e4d6 commit 6a8186f

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)