|
38 | 38 | */ |
39 | 39 | public class SimpleExpandableListAdapter extends BaseExpandableListAdapter { |
40 | 40 | private List<? extends Map<String, ?>> mGroupData; |
| 41 | + // Keeps track of if a group is currently expanded or not |
| 42 | + private boolean[] mIsGroupExpanded; |
41 | 43 | private int mExpandedGroupLayout; |
42 | 44 | private int mCollapsedGroupLayout; |
43 | 45 | private String[] mGroupFrom; |
@@ -196,6 +198,8 @@ public SimpleExpandableListAdapter(Context context, |
196 | 198 | int childLayout, int lastChildLayout, String[] childFrom, |
197 | 199 | int[] childTo) { |
198 | 200 | mGroupData = groupData; |
| 201 | + // Initially all groups are not expanded |
| 202 | + mIsGroupExpanded = new boolean[groupData.size()]; |
199 | 203 | mExpandedGroupLayout = expandedGroupLayout; |
200 | 204 | mCollapsedGroupLayout = collapsedGroupLayout; |
201 | 205 | mGroupFrom = groupFrom; |
@@ -298,4 +302,52 @@ public boolean hasStableIds() { |
298 | 302 | return true; |
299 | 303 | } |
300 | 304 |
|
| 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 | + } |
301 | 353 | } |
0 commit comments