From 3a774114bd32b183d541db7c9eb507d79df00f25 Mon Sep 17 00:00:00 2001 From: Mattias Flodin Date: Sun, 2 Dec 2012 10:24:24 +0100 Subject: [PATCH] Examples in demo project for single- and multi-choice listview --- demo/AndroidManifest.xml | 4 ++ demo/res/layout/checkable_main.xml | 21 +++++++ demo/res/layout/list_item_checkable.xml | 22 ++++++++ demo/res/layout/list_item_radio.xml | 22 ++++++++ demo/res/values/strings.xml | 9 +++ .../demodslv/CheckableLinearLayout.java | 38 +++++++++++++ .../demodslv/MultipleChoiceListView.java | 52 +++++++++++++++++ .../demodslv/SingleChoiceListView.java | 56 +++++++++++++++++++ 8 files changed, 224 insertions(+) create mode 100644 demo/res/layout/checkable_main.xml create mode 100644 demo/res/layout/list_item_checkable.xml create mode 100644 demo/res/layout/list_item_radio.xml create mode 100644 demo/src/com/mobeta/android/demodslv/CheckableLinearLayout.java create mode 100644 demo/src/com/mobeta/android/demodslv/MultipleChoiceListView.java create mode 100644 demo/src/com/mobeta/android/demodslv/SingleChoiceListView.java diff --git a/demo/AndroidManifest.xml b/demo/AndroidManifest.xml index 388eeed..1f83b9b 100644 --- a/demo/AndroidManifest.xml +++ b/demo/AndroidManifest.xml @@ -28,5 +28,9 @@ android:label="Sections" /> + + diff --git a/demo/res/layout/checkable_main.xml b/demo/res/layout/checkable_main.xml new file mode 100644 index 0000000..6419b36 --- /dev/null +++ b/demo/res/layout/checkable_main.xml @@ -0,0 +1,21 @@ + + + diff --git a/demo/res/layout/list_item_checkable.xml b/demo/res/layout/list_item_checkable.xml new file mode 100644 index 0000000..bdf7aae --- /dev/null +++ b/demo/res/layout/list_item_checkable.xml @@ -0,0 +1,22 @@ + + + + + diff --git a/demo/res/layout/list_item_radio.xml b/demo/res/layout/list_item_radio.xml new file mode 100644 index 0000000..c8d4318 --- /dev/null +++ b/demo/res/layout/list_item_radio.xml @@ -0,0 +1,22 @@ + + + + + diff --git a/demo/res/values/strings.xml b/demo/res/values/strings.xml index 30c32f4..04fab69 100644 --- a/demo/res/values/strings.xml +++ b/demo/res/values/strings.xml @@ -8,6 +8,8 @@ Background handle Sections CursorAdapter + Multiple-choice mode + Single-choice mode @@ -38,6 +40,13 @@ which abstracts drag-sorts with a remapping of Cursor positions to ListView positions. + + Uses Checkable list items in multiple-choice mode. + + + Uses Checkable list items to allow for selectable radio + buttons in single-choice mode. + OK Cancel diff --git a/demo/src/com/mobeta/android/demodslv/CheckableLinearLayout.java b/demo/src/com/mobeta/android/demodslv/CheckableLinearLayout.java new file mode 100644 index 0000000..8d272dc --- /dev/null +++ b/demo/src/com/mobeta/android/demodslv/CheckableLinearLayout.java @@ -0,0 +1,38 @@ +package com.mobeta.android.demodslv; + +import android.content.Context; +import android.util.AttributeSet; +import android.widget.Checkable; +import android.widget.LinearLayout; + +public class CheckableLinearLayout extends LinearLayout implements Checkable { + + private static final int CHECKABLE_CHILD_INDEX = 1; + private Checkable child; + + public CheckableLinearLayout(Context context, AttributeSet attrs) { + super(context, attrs); + } + + @Override + protected void onFinishInflate() { + super.onFinishInflate(); + child = (Checkable) getChildAt(CHECKABLE_CHILD_INDEX); + } + + @Override + public boolean isChecked() { + return child.isChecked(); + } + + @Override + public void setChecked(boolean checked) { + child.setChecked(checked); + } + + @Override + public void toggle() { + child.toggle(); + } + +} diff --git a/demo/src/com/mobeta/android/demodslv/MultipleChoiceListView.java b/demo/src/com/mobeta/android/demodslv/MultipleChoiceListView.java new file mode 100644 index 0000000..ddf7bc2 --- /dev/null +++ b/demo/src/com/mobeta/android/demodslv/MultipleChoiceListView.java @@ -0,0 +1,52 @@ +package com.mobeta.android.demodslv; + +import java.util.ArrayList; +import java.util.Arrays; + +import android.app.ListActivity; +import android.os.Bundle; +import android.widget.ArrayAdapter; + +import com.mobeta.android.dslv.DragSortListView; + + +public class MultipleChoiceListView extends ListActivity +{ + private ArrayAdapter adapter; + + private DragSortListView.DropListener onDrop = + new DragSortListView.DropListener() { + @Override + public void drop(int from, int to) { + if (from != to) { + DragSortListView list = getListView(); + String item = adapter.getItem(from); + adapter.remove(item); + adapter.insert(item, to); + list.moveCheckState(from, to); + } + } + }; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.checkable_main); + + String[] array = getResources().getStringArray(R.array.jazz_artist_names); + ArrayList arrayList = new ArrayList(Arrays.asList(array)); + + adapter = new ArrayAdapter(this, R.layout.list_item_checkable, R.id.text, arrayList); + + setListAdapter(adapter); + + DragSortListView list = getListView(); + list.setDropListener(onDrop); + } + + @Override + public DragSortListView getListView() { + return (DragSortListView) super.getListView(); + } + +} diff --git a/demo/src/com/mobeta/android/demodslv/SingleChoiceListView.java b/demo/src/com/mobeta/android/demodslv/SingleChoiceListView.java new file mode 100644 index 0000000..efa0dcf --- /dev/null +++ b/demo/src/com/mobeta/android/demodslv/SingleChoiceListView.java @@ -0,0 +1,56 @@ +package com.mobeta.android.demodslv; + +import java.util.ArrayList; +import java.util.Arrays; + +import android.app.ListActivity; +import android.os.Bundle; +import android.util.Log; +import android.widget.ArrayAdapter; +import android.widget.ListView; + +import com.mobeta.android.dslv.DragSortListView; + + +public class SingleChoiceListView extends ListActivity +{ + private ArrayAdapter adapter; + + private DragSortListView.DropListener onDrop = + new DragSortListView.DropListener() { + @Override + public void drop(int from, int to) { + if (from != to) { + DragSortListView list = getListView(); + String item = adapter.getItem(from); + adapter.remove(item); + adapter.insert(item, to); + list.moveCheckState(from, to); + Log.d("DSLV", "Selected item is " + list.getCheckedItemPosition()); + } + } + }; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.checkable_main); + + String[] array = getResources().getStringArray(R.array.jazz_artist_names); + ArrayList arrayList = new ArrayList(Arrays.asList(array)); + + adapter = new ArrayAdapter(this, R.layout.list_item_radio, R.id.text, arrayList); + + setListAdapter(adapter); + + DragSortListView list = getListView(); + list.setDropListener(onDrop); + list.setChoiceMode(ListView.CHOICE_MODE_SINGLE); + } + + @Override + public DragSortListView getListView() { + return (DragSortListView) super.getListView(); + } + +}