Skip to content

Commit

Permalink
Examples in demo project for single- and multi-choice listview
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattias Flodin committed Jan 8, 2013
1 parent e42d7ab commit 3a77411
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 0 deletions.
4 changes: 4 additions & 0 deletions demo/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@
android:label="Sections" />
<activity android:name="CursorDSLV"
android:label="Cursor-backed" />
<activity android:name="MultipleChoiceListView"
android:label="Multi-select list" />
<activity android:name="SingleChoiceListView"
android:label="Single-choice list" />
</application>
</manifest>
21 changes: 21 additions & 0 deletions demo/res/layout/checkable_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<com.mobeta.android.dslv.DragSortListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dslv="http://schemas.android.com/apk/res/com.mobeta.android.demodslv"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="3dp"
android:choiceMode="multipleChoice"
android:dividerHeight="1px"
android:padding="3dp"
dslv:click_remove_id="@id/click_remove"
dslv:collapsed_height="1px"
dslv:drag_enabled="true"
dslv:drag_handle_id="@id/drag_handle"
dslv:drag_scroll_start="0.33"
dslv:drag_start_mode="onDown"
dslv:float_alpha="0.6"
dslv:remove_enabled="true"
dslv:remove_mode="clickRemove"
dslv:slide_shuffle_speed="0.3" />

22 changes: 22 additions & 0 deletions demo/res/layout/list_item_checkable.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<com.mobeta.android.demodslv.CheckableLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="@dimen/item_height"
android:orientation="horizontal">
<ImageView
android:id="@id/drag_handle"
android:background="@drawable/drag"
android:layout_width="wrap_content"
android:layout_height="@dimen/item_height"
android:layout_weight="0" />
<CheckedTextView
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="@dimen/item_height"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center_vertical"
android:paddingLeft="8dp" />
</com.mobeta.android.demodslv.CheckableLinearLayout>
22 changes: 22 additions & 0 deletions demo/res/layout/list_item_radio.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<com.mobeta.android.demodslv.CheckableLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="@dimen/item_height"
android:orientation="horizontal">
<ImageView
android:id="@id/drag_handle"
android:background="@drawable/drag"
android:layout_width="wrap_content"
android:layout_height="@dimen/item_height"
android:layout_weight="0" />
<CheckedTextView
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="@dimen/item_height"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center_vertical"
android:paddingLeft="8dp" />
</com.mobeta.android.demodslv.CheckableLinearLayout>
9 changes: 9 additions & 0 deletions demo/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<item>Background handle</item>
<item>Sections</item>
<item>CursorAdapter</item>
<item>Multiple-choice mode</item>
<item>Single-choice mode</item>
</string-array>
<string-array name="activity_descs">
<item>
Expand Down Expand Up @@ -38,6 +40,13 @@
which abstracts drag-sorts with a remapping of Cursor
positions to ListView positions.
</item>
<item>
Uses Checkable list items in multiple-choice mode.
</item>
<item>
Uses Checkable list items to allow for selectable radio
buttons in single-choice mode.
</item>
</string-array>
<string name="ok">OK</string>
<string name="cancel">Cancel</string>
Expand Down
38 changes: 38 additions & 0 deletions demo/src/com/mobeta/android/demodslv/CheckableLinearLayout.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
52 changes: 52 additions & 0 deletions demo/src/com/mobeta/android/demodslv/MultipleChoiceListView.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> arrayList = new ArrayList<String>(Arrays.asList(array));

adapter = new ArrayAdapter<String>(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();
}

}
56 changes: 56 additions & 0 deletions demo/src/com/mobeta/android/demodslv/SingleChoiceListView.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> arrayList = new ArrayList<String>(Arrays.asList(array));

adapter = new ArrayAdapter<String>(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();
}

}

0 comments on commit 3a77411

Please sign in to comment.