Skip to content

Commit

Permalink
Support for Checkable list items
Browse files Browse the repository at this point in the history
When ListView has a choiceMode other than none, it can graphically
display which item(s) are currently selected, if (and only if) the list
items implement the Checkable interface. With DSLV, even if list items
implement this interface, it is hidden by the list-item wrapper used
(DragSortItemView).

With this change, DragSortItemView implements the Checkable interface
and forwards it to the wrapped item view if it implements Checkable,
allowing DSLV to function properly in all choice modes.

I considered a different design where we have a
CheckableDragSortItemView that inherits from DragSortItemView. DSLV
would then instantiate different variants depending on what is needed.
However, the only reason I could find for doing this would be to avoid
the few cycles spent on instanceof checks, and the original ListView
code does not seem to have any concerns about doing this (see
ListView.setupChild).
  • Loading branch information
Mattias Flodin committed Jan 8, 2013
1 parent 0ad9f5b commit 64b887d
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion library/src/com/mobeta/android/dslv/DragSortItemView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Checkable;
import android.util.Log;

/**
Expand All @@ -23,7 +24,7 @@
* The purpose of this class is to optimize slide
* shuffle animations.
*/
public class DragSortItemView extends ViewGroup {
public class DragSortItemView extends ViewGroup implements Checkable {

private int mGravity = Gravity.TOP;

Expand Down Expand Up @@ -97,4 +98,26 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(width, height);
}

@Override
public boolean isChecked() {
View child = getChildAt(0);
if (child instanceof Checkable)
return ((Checkable) child).isChecked();
else
return false;
}

@Override
public void setChecked(boolean checked) {
View child = getChildAt(0);
if (child instanceof Checkable)
((Checkable) child).setChecked(checked);
}

@Override
public void toggle() {
View child = getChildAt(0);
if (child instanceof Checkable)
((Checkable) child).toggle();
}
}

0 comments on commit 64b887d

Please sign in to comment.