Skip to content

Commit ea7e153

Browse files
author
Mattias Flodin
committed
DragSortListView.removeCheckState
Method similar to moveCheckState but for moving check statuses up one step when a list item was removed.
1 parent 3a77411 commit ea7e153

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

library/src/com/mobeta/android/dslv/DragSortListView.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,40 @@ public void moveCheckState(int from, int to) {
24992499
}
25002500
}
25012501

2502+
/**
2503+
* Use this when an item has been deleted, to move the check state of all
2504+
* preceding items up one step. If you have a choiceMode which is not none,
2505+
* this method must be called when the order of items changes in an
2506+
* underlying adapter which does not have stable IDs (see
2507+
* {@link ListAdapter#hasStableIds()}). This is because without IDs, the
2508+
* ListView has no way of knowing which items have moved where, and cannot
2509+
* update the check state accordingly.
2510+
*
2511+
* See also further comments on {@link #moveCheckState(int, int)}.
2512+
*
2513+
* @param position
2514+
*/
2515+
public void removeCheckState(int position) {
2516+
SparseBooleanArray cip = getCheckedItemPositions();
2517+
2518+
if (cip.size() == 0)
2519+
return;
2520+
int[] runStart = new int[cip.size()];
2521+
int[] runEnd = new int[cip.size()];
2522+
int rangeStart = position;
2523+
int rangeEnd = cip.keyAt(cip.size()-1)+1;
2524+
int runCount = buildRunList(cip, rangeStart, rangeEnd, runStart, runEnd);
2525+
for (int i=0; i!=runCount; i++) {
2526+
if (!(runStart[i] == position || (runEnd[i] < runStart[i] && runEnd[i] > position))) {
2527+
// Only set a new check mark in front of this run if it does
2528+
// not contain the deleted position. If it does, we only need
2529+
// to make it one check mark shorter at the end.
2530+
setItemChecked(rotate(runStart[i], - 1, rangeStart, rangeEnd), true);
2531+
}
2532+
setItemChecked(rotate(runEnd[i], - 1, rangeStart, rangeEnd), false);
2533+
}
2534+
}
2535+
25022536
private static int buildRunList(SparseBooleanArray cip, int rangeStart,
25032537
int rangeEnd, int[] runStart, int[] runEnd) {
25042538
int runCount = 0;

0 commit comments

Comments
 (0)