static ExpandableListPosition obtainGroupPosition(int groupPosition) { return obtain(GROUP, groupPosition, 0, 0); }
private static ExpandableListPosition getRecycledOrCreate() { ExpandableListPosition elp; synchronized (sPool) { if (sPool.size() > 0) { elp = sPool.remove(0); } else { return new ExpandableListPosition(); } } elp.resetState(); return elp; }
public static ExpandableListPosition obtain(int type, int groupPos, int childPos, int flatListPos) { ExpandableListPosition elp = getRecycledOrCreate(); elp.type = type; elp.groupPos = groupPos; elp.childPos = childPos; elp.flatListPos = flatListPos; return elp; }
/** * Converts a child position to a flat list position. * * @param packedPosition The child positions to be converted in it's * packed position representation. * @return The flat list position for the given child */ public int getFlattenedChildIndex(long packedPosition) { ExpandableListPosition listPosition = ExpandableListPosition.obtainPosition(packedPosition); return getFlattenedChildIndex(listPosition); }
static ExpandableListPosition obtainChildPosition(int groupPosition, int childPosition) { return obtain(CHILD, groupPosition, childPosition, 0); }
static ExpandableListPosition obtainPosition(long packedPosition) { if (packedPosition == ExpandableListView.PACKED_POSITION_VALUE_NULL) { return null; } ExpandableListPosition elp = getRecycledOrCreate(); elp.groupPos = ExpandableListView.getPackedPositionGroup(packedPosition); if (ExpandableListView.getPackedPositionType(packedPosition) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { elp.type = CHILD; elp.childPos = ExpandableListView.getPackedPositionChild(packedPosition); } else { elp.type = GROUP; } return elp; }
/** * 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); } }