Android ListView with drag and drop reordering.
Android ListView with drag and drop reordering.
I do not have much time to devote to this project so I am dropping support for the time being. Sorry everybody!
April 2, 2013: Hey all. If you want to send a pull request, please read the Contributing section first. Thanks!
Feb. 9, 2013: Version 0.6.0. Consolidated remove modes to
click_remove and fling_remove. No more fling remove while
dragging; fling anywhere on item to remove it.
Leszek Mzyk is a bona fide code-slanger.
Jan. 10, 2013: Version 0.5.0 is released. Supports ListView multi-choice and single-choice modes thanks to the hard work of Mattias Flodin! Awesome-sauce. Check out the new demos.
Nov. 23, 2012: Hmmm, what's this? → Gittip :)
Nov. 17, 2012: Drag-Sort Demos app in Google Play Store!
Nov. 15, 2012: Smooth operation! Drops and removals are animated. Also, DragSortController now provides a convenience click-to-remove feature (see XML attrs and RemoveListener sections).
Nov. 11, 2012: Mavenized. Thanks to Andreas Schildbach (Goonie)!
Oct. 30, 2012: DragSortCursorAdapter class helps you reorder a Cursor-backed ListAdapter. Look at ResourceDragSortCursorAdapter and SimpleDragSortCursorAdapter as well in the API.
Oct. 19, 2012: Public API documentation is up at http://bauerca.github.com/drag-sort-listview.
Oct. 19, 2012: Refactoring rampage. Backward compatibility is slightly broken. New features make it worthwhile :) and include: total floating View customization, total control over drag start/stop, and a helper class implementing common patterns (long-press to drag, fling-to-remove, etc.). Thanks to Dan Hulme (orac) for getting all this rolling! Check out the extensively updated demos and usage section below.
Sept. 26, 2012: Drag-sorting is now animated! (optional, of course) Items slide around underneath the floating (dragged) View.
DragSortListView (DSLV) is an extension of the Android ListView that enables drag-and-drop reordering of list items. It is a major overhaul complete rewrite of the TouchInterceptor (TI) meant to give drag-sorting a polished feel. Some key features are:
startDrag() and stopDrag() methods.DragSortListView is useful for all kinds of prioritized lists: favorites, playlists, checklists, etc. Would love to hear about your use case or app by email. I hope you find it useful; and please, help me improve the thing!
Three major elements define the drag-sort process. Roughly, in order of importance, they are:
startDrag() and
stopDrag() on your DSLV instance; but some help that is.
The convenience class, DragSortController, provides all kinds of
boiler-plate for common start/stop/remove drag patterns.Number 1 is essential. As mentioned above, 2 and 3 can be handled by the DragSortController helper class. Keep reading, then head to the demo and start studying some examples.
DragSortListView can be declared in an XML layout file just like the ListView. Several example layout files are provided in the demo. The available attributes (in addition to the usual ListView attributes) are given below. Read each bullet as
<xml attr>: (<datatype>, <default value>) <description>.collapsed_height: (dimension, 1px) Height of placeholder at original
drag position. Cannot be zero.drag_scroll_start: (float, 0.3) Start of drag-scroll regions
(defined by a
fraction of the total DSLV height; i.e. between 0 and 1).max_drag_scroll_speed: (float, 0.5) Maximum drag-scroll speed for
default linear drag-scroll profile. Units of pixels/millisecond.float_alpha: (float, 1.0) Transparency of floating View. Value from
0 to 1 where 1 is opaque.slide_shuffle_speed: (float, 0.7) Speed of shuffle animations
underneath floating View. A value
of 0 means a shuffle animation is always in progress, whereas a value
of 1 means items snap from position to position without animation.drop_animation_duration: (int, 150) Drop animation smoothly centers
the floating View over the drop slot before destroying it. Duration
in milliseconds.remove_animation_duration: (int, 150) Remove animation smoothly
collapses the empty slot when an item is removed. Duration
in milliseconds.track_drag_sort: (bool, false) Debugging option; explained below.use_default_controller: (bool, true) Have DSLV create a
DragSortController instance and pass the following xml attributes
to it. If you set this to false, ignore the following attributes.float_background_color: (color, BLACK) Set the background
color of the floating View when using the default
DragSortController. Floating View in this case is a snapshot of
the list item to be dragged.drag_handle_id: (id, 0) Android resource id that points to a
child View of a list item (or the root View of the list item
layout). This identifies the "drag handle," or the View within a
list item that must
be touched to start a drag-sort of that item.
Required if drags are to be enabled using the default
DragSortController.sort_enabled: (bool, true) Enable sorting of dragged item (disabling
is useful when you only want item removal).drag_start_mode: (enum, "onDown") Sets the gesture for starting
a drag.remove_enabled: (bool, false) Enable dragged item removal by one
of the remove_mode options below.remove_mode: (enum, "flingRight") Sets the gesture for removing the
dragged item.click_remove_id.click_remove_id: (id, 0) Android resource id that points to a
child View of a list item. When remove_mode="clickRemove" and
remove_enabled="true", a click on this child View removes the
containing item. This attr is used by DragSortController.fling_handle_id: (id, 0) Android resource id that points to a
child View of a list item. When remove_mode="flingRemove" and
remove_enabled="true", a fling that originates on this child
View removes the containing item. This attr is used by
DragSortController.DragSortListView is a ListView, and thus requires a ListAdapter to populate its items. Drag-sorting additionally implies a reordering of the items in the ListAdapter, achieved through callbacks to special Listener interfaces defined in DSLV. Listeners can be registered with DSLV in two ways:
set*Listener() methodsDragSortListView.setAdapter() is called
with your custom
Adapter, DSLV detects which interfaces are implemented and calls
the appropriate set*Listener() methods on itself with the
provided ListAdapter as argument.Each Listener interface is described below:
The DropListener interface has a single callback:
public void drop(int from, int to);
This is called upon completion of the drag-sort; i.e. when the
floating View is dropped.
The parameter from is the ListView item that was originally dragged,
and to is the position where the item was dropped.
This is an important callback; without
a DropListener, DSLV is for all practical purposes useless.
For proper DSLV operation, this callback must perform a reordering of the data in your ListAdapter. For example, one often has a Cursor that pulls from a database and backs a CursorAdapter. The order of items in the Cursor is fixed; therefore, given drag-sorting, you must implement a mapping from Cursor positions to DSLV positions. This is commonly done within in a custom ListAdapter or CursorWrapper that implements the DropListener interface. See Issue #20 for a discussion of this. Update: Or simply use the DragSortCursorAdapter class!
If your DSLV instance android:choiceMode is not "none", and your
ListAdapter does not have stable ids, you must call
[DragSortListView.moveCheckState(int from, int to)](http://bauerca.github.com/drag-sort-listview/reference/com/mobeta/android/dslv/DragSortListView.html#moveCheckState(int, int))
within drop(from, to). See the documentation in the DSLV API for more
info.
As the TI did, DSLV provides gestures for removing the floating View (and its associated list item) from the list. Upon completion of a remove gesture, DSLV calls the RemoveListener method:
public void remove(int which);
The position which should be "removed" from your ListAdapter; i.e.
the mapping from your data (e.g. in a Cursor) to your ListAdapter
should henceforth neglect the item previously pointed to by which.
Whether you actually remove the data or not is up to you.
Item removal can now happen outside of a drag event. The method
DragSortListView.removeItem(int position) can be called at any time.
If your DSLV instance android:choiceMode is not "none", and your
ListAdapter does not have stable ids, you must call
[DragSortListView.removeCheckState(int position)](http://bauerca.github.com/drag-sort-listview/reference/com/mobeta/android/dslv/DragSortListView.html#removeCheckState(int\))
within remove(which). See the documentation in the DSLV API for more
info.
The callback in the DragListener is
public void drag(int from, int to);
This is called whenever the floating View hovers to a new potential
drop position; to is the current potential drop position, and from is
the previous one. The TI provided this callback; an example of usage
does not come to mind.
This is a convenience interface which combines all of the above Listener interfaces.
This is the interface that handles creation, updates, and tear-downs
of the floating View. It is passed to DSLV using the
setFloatViewManager() method. Example usage can be found in
the SimpleFloatViewManager, which
is a convenience class
that simply takes a snapsho
Premium
Premium
NEW SortView
ok
Translation to Korean
No empty view when list view is empty
Show dialog confirm when fling to remove item
TextView
If I use baseAdapter,which basic sentences should I add in 'DragSortListener.drop'?
How to add this library in an Android Studio