static ExpandableListPosition obtainGroupPosition(int groupPosition) { return obtain(GROUP, groupPosition, 0, 0); }
static ExpandableListPosition obtainChildPosition(int groupPosition, int childPosition) { return obtain(CHILD, groupPosition, childPosition, 0); }
/** * Translates a flat list position (the raw position of an item (child or group) in the list) to * either a) group pos if the specified flat list position corresponds to a group, or b) child * pos if it corresponds to a child. Performs a binary search on the expanded * groups list to find the flat list pos if it is an exp group, otherwise * finds where the flat list pos fits in between the exp groups. * * @param flPos the flat list position to be translated * @return the group position or child position of the specified flat list * position encompassed in a {@link ExpandableListPosition} object * that contains additional useful info for insertion, etc. */ public ExpandableListPosition getUnflattenedPosition(int flPos) { int groupItemCount; int adapted = flPos; for (int i = 0; i < groups.size(); i++) { groupItemCount = numberOfVisibleItemsInGroup(i); if (adapted == 0) { return ExpandableListPosition.obtain(ExpandableListPosition.GROUP, i, -1, flPos); } else if (adapted < groupItemCount) { return ExpandableListPosition.obtain(ExpandableListPosition.CHILD, i, adapted - 1, flPos); } adapted -= groupItemCount; } throw new RuntimeException("Unknown state"); }
@Test public void test_getUnflattenedPosition() { ExpandableList list = new ExpandableList(groups); int flatPos = 3; //initial state //flatPos 3 == group at index 3 ExpandableListPosition initialExpected = obtain(GROUP, 3, -1, 3); ExpandableListPosition initialActual = list.getUnflattenedPosition(flatPos); assertEquals(initialExpected, initialActual); //expand first group list.expandedGroupIndexes[0] = true; //flatPos 3 == child number 2 within group at index 0 ExpandableListPosition newExpected = obtain(CHILD, 0, 2, 3); ExpandableListPosition newActual = list.getUnflattenedPosition(flatPos); assertEquals(newExpected, newActual); } }