Skip to content

Commit d0bb590

Browse files
committed
Merge remote-tracking branch 'mflodin/checkable' into checkable
Support checkable item removal. Conflicts: demo/res/layout/list_item_checkable.xml demo/src/com/mobeta/android/demodslv/MultipleChoiceListView.java library/src/com/mobeta/android/dslv/DragSortListView.java
2 parents 16e43e9 + ff93708 commit d0bb590

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

demo/res/layout/list_item_checkable.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@
1919
android:textAppearance="?android:attr/textAppearanceMedium"
2020
android:gravity="center_vertical"
2121
android:paddingLeft="8dp" />
22+
<ImageView
23+
android:id="@id/click_remove"
24+
android:background="@drawable/delete_x"
25+
android:layout_width="@dimen/item_height"
26+
android:layout_height="@dimen/item_height"
27+
android:layout_weight="0" />
2228
</com.mobeta.android.demodslv.CheckableLinearLayout>

demo/src/com/mobeta/android/demodslv/MultipleChoiceListView.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.widget.ArrayAdapter;
99

1010
import com.mobeta.android.dslv.DragSortListView;
11+
import com.mobeta.android.dslv.DragSortListView.RemoveListener;
1112

1213

1314
public class MultipleChoiceListView extends ListActivity
@@ -28,6 +29,17 @@ public void drop(int from, int to) {
2829
}
2930
};
3031

32+
private RemoveListener onRemove =
33+
new DragSortListView.RemoveListener() {
34+
@Override
35+
public void remove(int which) {
36+
DragSortListView list = getListView();
37+
String item = adapter.getItem(which);
38+
adapter.remove(item);
39+
list.removeCheckState(which);
40+
}
41+
};
42+
3143
@Override
3244
protected void onCreate(Bundle savedInstanceState) {
3345
super.onCreate(savedInstanceState);
@@ -42,6 +54,7 @@ protected void onCreate(Bundle savedInstanceState) {
4254

4355
DragSortListView list = getListView();
4456
list.setDropListener(onDrop);
57+
list.setRemoveListener(onRemove);
4558
}
4659

4760
@Override

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,6 +1941,7 @@ private void measureItem(View item) {
19411941
ViewGroup.LayoutParams lp = item.getLayoutParams();
19421942
if (lp == null) {
19431943
lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
1944+
item.setLayoutParams(lp);
19441945
}
19451946
int wspec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec, getListPaddingLeft() + getListPaddingRight(), lp.width);
19461947
int hspec;
@@ -2500,6 +2501,40 @@ public void moveCheckState(int from, int to) {
25002501
}
25012502
}
25022503

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

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.widget.ListView;
77
import android.widget.ImageView;
88
import android.view.View;
9+
import android.view.ViewGroup;
910
import android.util.Log;
1011

1112
/**
@@ -59,6 +60,7 @@ public View onCreateFloatView(int position) {
5960
mImageView.setBackgroundColor(mFloatBGColor);
6061
mImageView.setPadding(0, 0, 0, 0);
6162
mImageView.setImageBitmap(mFloatBitmap);
63+
mImageView.setLayoutParams(new ViewGroup.LayoutParams(v.getWidth(), v.getHeight()));
6264

6365
return mImageView;
6466
}

0 commit comments

Comments
 (0)