Skip to content

Commit 3a77411

Browse files
author
Mattias Flodin
committed
Examples in demo project for single- and multi-choice listview
1 parent e42d7ab commit 3a77411

File tree

8 files changed

+224
-0
lines changed

8 files changed

+224
-0
lines changed

demo/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,9 @@
2828
android:label="Sections" />
2929
<activity android:name="CursorDSLV"
3030
android:label="Cursor-backed" />
31+
<activity android:name="MultipleChoiceListView"
32+
android:label="Multi-select list" />
33+
<activity android:name="SingleChoiceListView"
34+
android:label="Single-choice list" />
3135
</application>
3236
</manifest>

demo/res/layout/checkable_main.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<com.mobeta.android.dslv.DragSortListView xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:dslv="http://schemas.android.com/apk/res/com.mobeta.android.demodslv"
4+
android:id="@android:id/list"
5+
android:layout_width="fill_parent"
6+
android:layout_height="fill_parent"
7+
android:layout_margin="3dp"
8+
android:choiceMode="multipleChoice"
9+
android:dividerHeight="1px"
10+
android:padding="3dp"
11+
dslv:click_remove_id="@id/click_remove"
12+
dslv:collapsed_height="1px"
13+
dslv:drag_enabled="true"
14+
dslv:drag_handle_id="@id/drag_handle"
15+
dslv:drag_scroll_start="0.33"
16+
dslv:drag_start_mode="onDown"
17+
dslv:float_alpha="0.6"
18+
dslv:remove_enabled="true"
19+
dslv:remove_mode="clickRemove"
20+
dslv:slide_shuffle_speed="0.3" />
21+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<com.mobeta.android.demodslv.CheckableLinearLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:layout_width="fill_parent"
5+
android:layout_height="@dimen/item_height"
6+
android:orientation="horizontal">
7+
<ImageView
8+
android:id="@id/drag_handle"
9+
android:background="@drawable/drag"
10+
android:layout_width="wrap_content"
11+
android:layout_height="@dimen/item_height"
12+
android:layout_weight="0" />
13+
<CheckedTextView
14+
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
15+
android:id="@+id/text"
16+
android:layout_width="wrap_content"
17+
android:layout_height="@dimen/item_height"
18+
android:layout_weight="1"
19+
android:textAppearance="?android:attr/textAppearanceMedium"
20+
android:gravity="center_vertical"
21+
android:paddingLeft="8dp" />
22+
</com.mobeta.android.demodslv.CheckableLinearLayout>

demo/res/layout/list_item_radio.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<com.mobeta.android.demodslv.CheckableLinearLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:layout_width="fill_parent"
5+
android:layout_height="@dimen/item_height"
6+
android:orientation="horizontal">
7+
<ImageView
8+
android:id="@id/drag_handle"
9+
android:background="@drawable/drag"
10+
android:layout_width="wrap_content"
11+
android:layout_height="@dimen/item_height"
12+
android:layout_weight="0" />
13+
<CheckedTextView
14+
android:checkMark="?android:attr/listChoiceIndicatorSingle"
15+
android:id="@+id/text"
16+
android:layout_width="wrap_content"
17+
android:layout_height="@dimen/item_height"
18+
android:layout_weight="1"
19+
android:textAppearance="?android:attr/textAppearanceMedium"
20+
android:gravity="center_vertical"
21+
android:paddingLeft="8dp" />
22+
</com.mobeta.android.demodslv.CheckableLinearLayout>

demo/res/values/strings.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<item>Background handle</item>
99
<item>Sections</item>
1010
<item>CursorAdapter</item>
11+
<item>Multiple-choice mode</item>
12+
<item>Single-choice mode</item>
1113
</string-array>
1214
<string-array name="activity_descs">
1315
<item>
@@ -38,6 +40,13 @@
3840
which abstracts drag-sorts with a remapping of Cursor
3941
positions to ListView positions.
4042
</item>
43+
<item>
44+
Uses Checkable list items in multiple-choice mode.
45+
</item>
46+
<item>
47+
Uses Checkable list items to allow for selectable radio
48+
buttons in single-choice mode.
49+
</item>
4150
</string-array>
4251
<string name="ok">OK</string>
4352
<string name="cancel">Cancel</string>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.mobeta.android.demodslv;
2+
3+
import android.content.Context;
4+
import android.util.AttributeSet;
5+
import android.widget.Checkable;
6+
import android.widget.LinearLayout;
7+
8+
public class CheckableLinearLayout extends LinearLayout implements Checkable {
9+
10+
private static final int CHECKABLE_CHILD_INDEX = 1;
11+
private Checkable child;
12+
13+
public CheckableLinearLayout(Context context, AttributeSet attrs) {
14+
super(context, attrs);
15+
}
16+
17+
@Override
18+
protected void onFinishInflate() {
19+
super.onFinishInflate();
20+
child = (Checkable) getChildAt(CHECKABLE_CHILD_INDEX);
21+
}
22+
23+
@Override
24+
public boolean isChecked() {
25+
return child.isChecked();
26+
}
27+
28+
@Override
29+
public void setChecked(boolean checked) {
30+
child.setChecked(checked);
31+
}
32+
33+
@Override
34+
public void toggle() {
35+
child.toggle();
36+
}
37+
38+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.mobeta.android.demodslv;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
6+
import android.app.ListActivity;
7+
import android.os.Bundle;
8+
import android.widget.ArrayAdapter;
9+
10+
import com.mobeta.android.dslv.DragSortListView;
11+
12+
13+
public class MultipleChoiceListView extends ListActivity
14+
{
15+
private ArrayAdapter<String> adapter;
16+
17+
private DragSortListView.DropListener onDrop =
18+
new DragSortListView.DropListener() {
19+
@Override
20+
public void drop(int from, int to) {
21+
if (from != to) {
22+
DragSortListView list = getListView();
23+
String item = adapter.getItem(from);
24+
adapter.remove(item);
25+
adapter.insert(item, to);
26+
list.moveCheckState(from, to);
27+
}
28+
}
29+
};
30+
31+
@Override
32+
protected void onCreate(Bundle savedInstanceState) {
33+
super.onCreate(savedInstanceState);
34+
setContentView(R.layout.checkable_main);
35+
36+
String[] array = getResources().getStringArray(R.array.jazz_artist_names);
37+
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(array));
38+
39+
adapter = new ArrayAdapter<String>(this, R.layout.list_item_checkable, R.id.text, arrayList);
40+
41+
setListAdapter(adapter);
42+
43+
DragSortListView list = getListView();
44+
list.setDropListener(onDrop);
45+
}
46+
47+
@Override
48+
public DragSortListView getListView() {
49+
return (DragSortListView) super.getListView();
50+
}
51+
52+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.mobeta.android.demodslv;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
6+
import android.app.ListActivity;
7+
import android.os.Bundle;
8+
import android.util.Log;
9+
import android.widget.ArrayAdapter;
10+
import android.widget.ListView;
11+
12+
import com.mobeta.android.dslv.DragSortListView;
13+
14+
15+
public class SingleChoiceListView extends ListActivity
16+
{
17+
private ArrayAdapter<String> adapter;
18+
19+
private DragSortListView.DropListener onDrop =
20+
new DragSortListView.DropListener() {
21+
@Override
22+
public void drop(int from, int to) {
23+
if (from != to) {
24+
DragSortListView list = getListView();
25+
String item = adapter.getItem(from);
26+
adapter.remove(item);
27+
adapter.insert(item, to);
28+
list.moveCheckState(from, to);
29+
Log.d("DSLV", "Selected item is " + list.getCheckedItemPosition());
30+
}
31+
}
32+
};
33+
34+
@Override
35+
protected void onCreate(Bundle savedInstanceState) {
36+
super.onCreate(savedInstanceState);
37+
setContentView(R.layout.checkable_main);
38+
39+
String[] array = getResources().getStringArray(R.array.jazz_artist_names);
40+
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(array));
41+
42+
adapter = new ArrayAdapter<String>(this, R.layout.list_item_radio, R.id.text, arrayList);
43+
44+
setListAdapter(adapter);
45+
46+
DragSortListView list = getListView();
47+
list.setDropListener(onDrop);
48+
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
49+
}
50+
51+
@Override
52+
public DragSortListView getListView() {
53+
return (DragSortListView) super.getListView();
54+
}
55+
56+
}

0 commit comments

Comments
 (0)