An easy to use Drag & Drop List for Android. Direct replacement of the android ListView.

Overview

DragNDropListView

DragNDropListView is a direct replacement for the stock Android ListView. If you know how to use ListView, you already know how to use DragNDropListView. All you have to do is replace ListView and SimpleCursorAdapter/SimpleAdapter with DragNDropListView and its adapters.

Usage

DragNDropListView is an Android Library project. If you use Eclipse do the following.

  • Clone git clone git://github.com/terlici/DragNDropList.git
  • Import it in Eclipse
  • Add DragNDropList as a library to your Android project

Layout

A common layout for list view is the following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

Just replace ListView with com.terlici.dragndroplist.DragNDropListView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.terlici.dragndroplist.DragNDropListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

Row Layout

In DragNDropListView each item has a drag handler, which the users touch to drag the item around.

Here is a common layout for each row. It contains an image of a star and some text. The user can reorder the items by touching the star of an item and dragging it around.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80px"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    >

    <ImageView
        android:id="@+id/handler"
        android:layout_width="60px"
        android:layout_height="60px"
        android:src="@android:drawable/btn_star_big_on"
        android:layout_marginLeft="8px"
        />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Loading DragNDropListView

This is the usual way to load a ListView:

ListView list = (ListView)findViewById(android.R.id.list);

SimpleCursorAdapter adapter = new SimpleCursorAdapter(context,
                        R.layout.row,
                        cursor,
                        new String[]{"text"},
                        new int[]{R.id.text},
                        0);

list.setListAdapter(adapter);

When using DragNDropListView just replace ListView with DragNDropListView, SimpleCursorAdapter with DragNDropCursorAdapter and set the id of the drag handler as the last parameter of the adapter. In the row layout above this will be R.id.handler. Last, instead of setListAdapter use setDragNDropAdapter.

DragNDropListView list = (DragNDropListView)findViewById(android.R.id.list);

DragNDropCursorAdapter adapter = new DragNDropCursorAdapter(context,
                           R.layout.row,
                           cursor,
                           new String[]{"text"},
                           new int[]{R.id.text},
                           R.id.handler);

list.setDragNDropAdapter(adapter);

You are done! As you see there is very little to change to your existing code.

Events

DragNDropListView provides an event listener for drag and drop.

list.setOnItemDragNDropListener(...)

The interface for this listener is:

public interface OnItemDragNDropListener {
    // Called when the item begins dragging.
    public void onItemDrag(DragNDropListView parent, View view, int position, long id);

    // Called after the item is dropped in place
    public void onItemDrop(DragNDropListView parent, View view, int startPosition, int endPosition, long id);
}

This event is useful when storing the reordered items.

Adapters

DragNDropList comes with two adapters. The first one is DragNDropCursorAdapter which is a direct replacement for SimpleCursorAdapter. The second is DragNDropSimpleAdapter which is a replacement for SimpleAdapter.

Apps that use DragNDropList

DragNDropList was originally created for our apps Tasks and Tasks Pro. Download one of them to see it in action.

If other apps use it, please let us know and we will include them here.

Contribution

If you want to contribute, there are two more projects. DragNDropListApp implements a basic demo of the DragNDropList. DragNDropListAppTest runs tests on the DragNDropListApp and contains all the tests for the DragNDropList.

If you want to contribute, please fork also those two projects and add your tests to DragNDropListAppTest.

License

DragNDropList's code uses the Apache license, see our LICENSE file.

Comments
  • -Also Fixed issue related to DragNDropSimpleadapter background data list...

    -Also Fixed issue related to DragNDropSimpleadapter background data list...

    -Fixed issue ( notifyDataSetChanged was not working when background data attached with DragNDropSimpleadapter was changed). This was causing the app to crash due to index out of bound exception.

    • Updated Support Library Version to 21.0.3
    • Updated Android Target Version to 5.0.1
    opened by krazykira 4
  • Various fixes

    Various fixes

    Now the build system of Android is gradle so I added the build.gradle script.

    In the DragNDropListView factorize the call to the getSystemService(Context.WINDOW_SERVICE) so to not waste CPU cycle to obtain something already calculated.

    opened by gipi 0
  • Check the drop point is not a footer.

    Check the drop point is not a footer.

    I think this patch addresses the crash caused from dropping an item on the footer when present.

     08-01 17:49:09.628  30242-30242/? E/MessageQueue-JNI: java.lang.ArrayIndexOutOfBoundsException: length=7; index=7
     at com.terlici.dragndroplist.DragNDropCursorAdapter.onItemDrop(DragNDropCursorAdapter.java:97)
     at com.terlici.dragndroplist.DragNDropListView.stopDrag(DragNDropListView.java:249)
     at com.terlici.dragndroplist.DragNDropListView.onTouchEvent(DragNDropListView.java:156)
    
    opened by gipi 0
  • Fix crash when the ListView has a header/footer.

    Fix crash when the ListView has a header/footer.

    If the ListView has a footer a ClassCastException is thrown

    E/AndroidRuntime(17730): java.lang.ClassCastException: android.widget.HeaderViewListAdapter cannot be cast to com.terlici.dragndroplist.DragNDropAdapter
    E/AndroidRuntime(17730):    at com.terlici.dragndroplist.DragNDropListView.startDrag(DragNDropListView.java:163)
    E/AndroidRuntime(17730):    at com.terlici.dragndroplist.DragNDropListView.onTouchEvent(DragNDropListView.java:127)
    E/AndroidRuntime(17730):    at android.view.View.dispatchTouchEvent(View.java:7253)
    

    This branch tries to address this problem.

    opened by gipi 0
  • Fix drag handler position calculation

    Fix drag handler position calculation

    I noticed, that if you set the drag handler to a view whose parent is not the view generated by the adapter, the dragging-enabled area is not calculated right.

    To test this replace the content of testitem.xml (DragNDropListApp) with

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/handler"
            android:layout_width="60px"
            android:layout_height="60px"
            android:src="@android:drawable/btn_star_big_on"
            android:layout_marginLeft="8px"
            />
    </FrameLayout>
    
    opened by Helco 0
  • Drag could not start....

    Drag could not start....

    Hi

    with Nexus 7 , To start drag a list was easy. I just touch a list and just drag.

    with Nexus 5x ( android 6 ), To start drag is not easy. Almost case, I can not drag at all.

    Any idea ?

    opened by bidasknakayama 1
  • java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setDrawingCacheEnabled(boolean)' on a null object reference

    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setDrawingCacheEnabled(boolean)' on a null object reference

    Got a crash from one user. Making a null pointer check will solve this issue I guess in stopDrag(), e.g.

            if (item != null) {
                item.setDrawingCacheEnabled(false);
                item.destroyDrawingCache();
    
                item.setVisibility(View.VISIBLE);
            }
    

    Android: 5.0.1 Manufacturer: samsung Model: SM-N915G

    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setDrawingCacheEnabled(boolean)' on a null object reference
        at com.terlici.dragndroplist.DragNDropListView.stopDrag(SourceFile:265)
        at com.terlici.dragndroplist.DragNDropListView.onTouchEvent(SourceFile:162)
        at android.view.View.dispatchTouchEvent(View.java:8975)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2698)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2410)
        at android.widget.AbsListView.dispatchTouchEvent(AbsListView.java:5308)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
      [SKIPPED]
    
    opened by alexeyvasilyev 0
  • Handler appearing outside of the list

    Handler appearing outside of the list

    I used the same layouts for row and main activity as they are on the main page, except I changed the list width to 200dp.

    When I do this and start dragging the view, the star (drag handler) shows up on the wrong place. Instead of showing where the it is shown in the list it shows up in the middle of the screen.

    Here is the layout file:

    <com.terlici.dragndroplist.DragNDropListView
        android:id="@id/android:list"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />
    

    Here is the row layout:

    <ImageView
        android:id="@+id/handler"
        android:layout_width="60px"
        android:layout_height="60px"
        android:src="@android:drawable/btn_star_big_on"
        android:layout_marginLeft="8px"
        />
    
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    

    And here is the main activity code: package com.example.ddexample;

    import java.util.ArrayList; import java.util.Hashtable;

    import android.app.Activity; import android.content.Context; import android.os.Bundle;

    import com.terlici.dragndroplist.DragNDropListView; import com.terlici.dragndroplist.DragNDropSimpleAdapter;

    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        DragNDropListView list = (DragNDropListView) findViewById(android.R.id.list);
    
        ArrayList<Hashtable<String, String>> xxx = new ArrayList<Hashtable<String, String>>();
        for (int a = 0; a < 100; a++)
        {
            Hashtable<String, String> fsa = new Hashtable<>();
            fsa.put(String.valueOf(a), String.valueOf(a));
    
            xxx.add(fsa);
        }
        DragNDropSimpleAdapter adapter = new DragNDropSimpleAdapter((Context) this, xxx, R.layout.row, new String[] { "1" }, new int[] { R.id.text },
                R.id.handler);
    
        list.setDragNDropAdapter(adapter);
    
    }
    

    }

    opened by ak83 0
Owner
null
Android library to display a ListView whose cells are not rigid but flabby and react to ListView scroll.

FlabbyListView This library is not maintained anymore and there will be no further releases Android library to display a ListView which cells are not

JPARDOGO 762 Nov 23, 2022
Easy to use ListView with pinned sections for Android.

Easy to use ListView with pinned sections for Android 2.1 and higher. Pinned section is a header view which sticks to the top of the list until at lea

Sergej Shafarenka 2.6k Dec 21, 2022
An Android custom ListView and ScrollView with pull to zoom-in.

PullZoomView An Android custom ListView and ScrollView with pull to zoom-in. Features Set ZoomView enable Add HeaderView Custom ZoomView Parallax or N

Frank-Zhu 2.3k Dec 26, 2022
Android widget with pull to refresh for all the views,and support loadMore for ListView , RecyclerView, GridView and SwipeRefreshLayout.

CommonPullToRefresh Android widget with pull to refresh for all the views,and support loadMore for ListView,RecyclerView,GridView and SwipeRefreshLayo

null 1.1k Nov 10, 2022
A ListView with pinned section headers for Android

PinnedHeaderListView This library provides a sectioned ListView with pinned headers. It looks and feels much like the default contacts app does on And

James Smith 665 Nov 29, 2022
A generic, customizable, open source Android ListView implementation that has 'Pull to Refresh' functionality.

Android 'Pull to Refresh' ListView library Demo video: http://www.youtube.com/watch?v=VjmdELnm3GI Project A generic, customizable, open source Android

Erik Wallentinsen 639 Nov 17, 2022
Parallax ScrollView and ListView for Android

Parallax Scrolls Parallax ListView and ScrollView for Android This project includes ScrollView with one or more parallaxed views ListView with paralla

Nir Hartmann 851 Dec 3, 2022
android custom listview,with interaction pattern load more and pull to refresh to load data dinamically

The first thing that i have to say is render thanks to johannilsson because all the part of pull to refresh listview is based in the code of his repos

Fabian Leon 447 Nov 15, 2022
Awesome Listview filter functionality in Android.

About Awesome Listview filter functionality in Android. See it in Action: https://www.youtube.com/watch?v=RO54U1ES5CA Listview with beautiful transpar

Bhavya  Mehta 446 Nov 20, 2022
Bringing extended scrolling features to Android's native ListView and ExpandableListView.

About QuickScroll QuickScroll is a library aimed at creating similar scrolling experiences to the native Contacts app's People view. It has the same s

Andras Kindler 461 Nov 11, 2022
Android ListView with sticky headers

DEPRECATED HeaderListView is deprecated. No new development will be taking place. Quickstart Import the HeaderListView module in your Android Studio p

Applidium 314 Nov 10, 2022
Simple ListView based Android AccordionView

Android Accordion View Example git pull import to eclipse properties->android uncheck is library run as android application See main.xml. Screenshot L

Maciej Lopacinski 164 Nov 28, 2022
Android ListView that implements the QuickReturn UI pattern. Written from scratch with focus on performance.

QuickReturn Android ListView that implements the QuickReturn UI pattern. Written from scratch with focus on performance. Demo Usage In your build.grad

Felipe Lima 191 Nov 25, 2022
AssignmentListView is an Android ListView widget which calculate image loading times.

AssignmentListView is an Android ListView widget which calculate image loading times.

null 5 Sep 9, 2022
[] A swipe menu for ListView.

SwipeMenuListView A swipe menu for ListView. Demo Usage Add dependency dependencies { compile 'com.baoyz.swipemenulistview:library:1.3.0' } Step 1

ζ˜ŸδΈ€ 3.5k Dec 16, 2022
Horizontal list view for Android which allows variable items widths

Deprecated This widget is now deprecated and it won't be updated anymore. Use RecyclerView instead Horizontal Variable ListView Horizontal ListView fo

Alessandro Crugnola 862 Nov 15, 2022
A better ExpandableListView, with animated expandable views for each list item

SlideExpandableListView for Android Not happy with the Android ExpandableListView android offers? Want something like the Spotify app. This library al

Tjerk Wolterink 2k Dec 22, 2022
Android library to achieve in an easy way, the behaviour of the home page in the Expedia app, with a pair of auto-scroll circular parallax ListViews.

ListBuddies This library is not maintained anymore and there will be no further releases Android library of a pair of auto-scroll circular parallax Li

JPARDOGO 970 Dec 29, 2022
Expandable Recyclerview makes it easy to integrate nested recycler view...πŸ”¨ πŸ“

SSExpandableRecyclerView Expandable Recyclerview make it easy to integrate nested recyclerview Features Simple and easy to use ( no complex adapter re

Simform Solutions 52 Nov 1, 2022