Drag and Drop ListView to change item order - Tutorial (source code download)

Updated: 12 April 2013

Tested in:
HTC desire S (android 2.3.5)

Base on:
Eclipse Indigo build in 20120216-1857, Java 1.6, Android 4.1.2

Download Source code:
DemoDragDropListView20130412.rar

Description:
- Change items order by drag
- Fling/click to remove item

Screen shot:


Tutorial:
1. To implement this code, copy all files in wiget package and customize the layout of items, note that some files in res/values are needed, don't forget them.

2. You can change some important options of DragSortListView by modify:
<com.example.demodragdroplistview.widget.DragSortListView

        xmlns:dslv="http://schemas.android.com/apk/res/com.example.demodragdroplistview"

        android:id="@+id/simpleDragListview"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:cacheColorHint="#00ffffff"

        android:layout_alignParentTop="true"

        android:layout_above="@id/textMine"

        android:layout_margin="0dp"

        android:dividerHeight="1dp"

        android:divider="#55ffffff"

        android:paddingBottom="0dp"

        android:paddingLeft="0dp"

        android:paddingTop="0dp"

        dslv:collapsed_height="2dp"

        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:max_drag_scroll_speed="0.5"

        dslv:remove_enabled="true"

        dslv:remove_mode="flingRemove"

        dslv:slide_shuffle_speed="0.3"

        dslv:sort_enabled="true"

        dslv:track_drag_sort="false"

        dslv:use_default_controller="true" />



9 Responses to Drag and Drop ListView to change item order - Tutorial (source code download)

  1. Hilary says:

    Hi,
    Your download link doesn't appear to work - it takes me to a site that's trying to install a whole bunch of random stuff on my computer. Can you move the download link to somewhere where it's actually possible to view the code?

    Thanks!

  2. I checked the link, it worked right. If you can't, use proxy to access this file.
    Or enter your email, i will send the code to you.
    Many Thanks!

  3. Hi,

    I also have the same problem with Hilary, could you please send me the code ?. Thanks in advance.

  4. @George JR: give me your email :D or you send me an email to monopoly.vn@gmail.com

  5. Anonymous says:

    your download link not working

  6. Anonymous says:

    my email azahar@jeettechnosolutions.com

  7. Dear all,
    I replaced all download links to MediaFire, they are working well.

    Thank you for visiting my blog!

  8. Hi, first of all, thank you for your code.

    I downloaded and tried your project, but it doesn't seem to work fine. I can drag any item, but when I drop it, it comes back to the original position instead of staying on the new one. Am I doing something wrong?

  9. Thanks monopoly, this example was a lot simpler to understand than the demo with the library.

    Alberto, look here https://github.com/bauerca/drag-sort-listview for the notes on DropListener.

    In DragDropListView.java, underneath
    listView.setRemoveListener(onRemove);
    add
    listView.setDropListener(onDrop);

    then add this method in (can't see how to format this comment, so sorry for no indentation)

    private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() {

    @Override
    public void drop(int from, int to) {
    if (from > to) {
    // moving item up
    for (int i = from - 1; i >= to; i--) {
    Collections.swap(list, i, i + 1);
    }
    } else {
    // moving item down
    for (int i = from; i < to; i++) {
    Collections.swap(list, i, i + 1);
    }
    }
    simpleAdapter.notifyDataSetChanged();
    }
    };

Leave a Reply

Powered by Blogger.