Skip to content

Commit

Permalink
DragSortListView.removeCheckState
Browse files Browse the repository at this point in the history
Method similar to moveCheckState but for moving check statuses up one step
when a list item was removed.
  • Loading branch information
Mattias Flodin committed Jan 8, 2013
1 parent 3a77411 commit ea7e153
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions library/src/com/mobeta/android/dslv/DragSortListView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2499,6 +2499,40 @@ public void moveCheckState(int from, int to) {
}
}

/**
* Use this when an item has been deleted, to move the check state of all
* preceding items up one step. If you have a choiceMode which is not none,
* this method must be called when the order of items changes in an
* underlying adapter which does not have stable IDs (see
* {@link ListAdapter#hasStableIds()}). This is because without IDs, the
* ListView has no way of knowing which items have moved where, and cannot
* update the check state accordingly.
*
* See also further comments on {@link #moveCheckState(int, int)}.
*
* @param position
*/
public void removeCheckState(int position) {
SparseBooleanArray cip = getCheckedItemPositions();

if (cip.size() == 0)
return;
int[] runStart = new int[cip.size()];
int[] runEnd = new int[cip.size()];
int rangeStart = position;
int rangeEnd = cip.keyAt(cip.size()-1)+1;
int runCount = buildRunList(cip, rangeStart, rangeEnd, runStart, runEnd);
for (int i=0; i!=runCount; i++) {
if (!(runStart[i] == position || (runEnd[i] < runStart[i] && runEnd[i] > position))) {
// Only set a new check mark in front of this run if it does
// not contain the deleted position. If it does, we only need
// to make it one check mark shorter at the end.
setItemChecked(rotate(runStart[i], - 1, rangeStart, rangeEnd), true);
}
setItemChecked(rotate(runEnd[i], - 1, rangeStart, rangeEnd), false);
}
}

private static int buildRunList(SparseBooleanArray cip, int rangeStart,
int rangeEnd, int[] runStart, int[] runEnd) {
int runCount = 0;
Expand Down

0 comments on commit ea7e153

Please sign in to comment.