41

Bauerca drag-sort-listview is an amazing library for the drag functionality in a list view.. https://github.com/bauerca/drag-sort-listview

BUT my problem is that the demo project is pretty complex, I cant track it to include the functionality in my project..

Any simple example would be appreciated..

For example I just need a screen with this custom list that contains three items.. Or any other simple example..

Thank you

AntounG
  • 543
  • 1
  • 6
  • 9

1 Answers1

56

Here's a short program of how to use the library that I just managed to write myself. Basically it's the same thing as the sample, just all in one place.

package com.example.dndlist;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;


import com.mobeta.android.dslv.DragSortController;
import com.mobeta.android.dslv.DragSortListView;

public class MainActivity extends Activity
{ 
    DragSortListView listView;
    ArrayAdapter<String> adapter;

    private DragSortListView.DropListener onDrop = new DragSortListView.DropListener()
    {
        @Override
        public void drop(int from, int to)
        {
            if (from != to)
            {
                String item = adapter.getItem(from);
                adapter.remove(item);
                adapter.insert(item, to);
            }
        }
    };

    private DragSortListView.RemoveListener onRemove = new DragSortListView.RemoveListener()
    {
        @Override
        public void remove(int which)
        {
            adapter.remove(adapter.getItem(which));
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (DragSortListView) findViewById(R.id.listview);
        String[] names = getResources().getStringArray(R.array.random_names);
        ArrayList<String> list = new ArrayList<String>(Arrays.asList(names));
        adapter = new ArrayAdapter<String>(this,
                R.layout.item_layout, R.id.textView1, list);
        listView.setAdapter(adapter);
        listView.setDropListener(onDrop);
        listView.setRemoveListener(onRemove);

        DragSortController controller = new DragSortController(listView);
        controller.setDragHandleId(R.id.imageView1);
                //controller.setClickRemoveId(R.id.);
        controller.setRemoveEnabled(false);
        controller.setSortEnabled(true);
        controller.setDragInitMode(1);
                //controller.setRemoveMode(removeMode);

        listView.setFloatViewManager(controller);
        listView.setOnTouchListener(controller);
        listView.setDragEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:dslv="http://schemas.android.com/apk/res/com.example.dndlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<com.mobeta.android.dslv.DragSortListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="10dp"
    android:dividerHeight="5dp"
    android:paddingBottom="0dp"
    android:paddingLeft="10dp"
    android:paddingTop="0dp"
    dslv:collapsed_height="2dp"
    dslv:drag_enabled="true"
    dslv:drag_handle_id="@drawable/drag_handle" 
    dslv:drag_scroll_start="0.33"
    dslv:drag_start_mode="onMove"
    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" />
</RelativeLayout>
albciff
  • 18,112
  • 4
  • 64
  • 89
Lescai Ionel
  • 4,216
  • 3
  • 30
  • 48
  • Thanks a lot :) For your simple answer (Y) Although I managed to implement my own drag-n-drop list but this one is still the best one with great effects (Y) – AntounG Feb 23 '13 at 19:20
  • I got a lot of errors first time I tried to use the code. But got it fixed in the meantime. – schlingel Sep 30 '13 at 18:03
  • That's just the namespace I used for the library. It's defined in the 3rd row of the xml: `xmlns:dslv=...` – Lescai Ionel Oct 04 '13 at 14:07
  • Actually, I figured it out. I just needed to add a + to the "@id/drag_handle" and everything sorted itself out. – S Fitz Oct 04 '13 at 14:15
  • Also, remember to change the line mentioned above from the package com.example.dndlist, to the package name used in your app – Andrew Fielden Oct 13 '13 at 11:54
  • My list is not draggable. I suspect it has something to do with dslv:drag_handle_id="@drawable/drag_handle". What drawable did you use? – Lunchbox Feb 07 '14 at 11:24
  • 2
    just add an image of 3 horizontal lines or something – Lescai Ionel Feb 07 '14 at 13:00
  • @LescaiIonel, thank you. Everything works fine, the drag is working but when I drop nothing gets actualised like if the item wasn't moved. I've did on the onRemove Listener `adapter.notifyDataSetChanged()` but still nothing. Do you have any idea about this ? –  Feb 25 '14 at 02:35
  • @SFitz Don't add a '+'! The id "drag_handle" is created in ids.xml, the idea being to create a custom ID value that Android will ensure is unique. (Late hit I know, this is for all those who come after.) – William T. Mallard Aug 13 '14 at 15:31
  • @Lunchbox See my other comment, if you added a '+' to get a clean compile you shouldn't have. Make sure you have a drag_handle ID defined in ids.xml. – William T. Mallard Aug 13 '14 at 15:33
  • Just quickly having a mind boggle session.. (disregard this comment) – Lunchbox Aug 14 '14 at 11:51
  • Thanks alot, Worked for me I will share a tutorial based on this Answer very soon, that will help you guys – Naveed Ahmad Mar 03 '15 at 13:59
  • @NaveedAhmad: where is a link? You wrote an address of this question. – CoolMind Nov 24 '15 at 09:25
  • 1
    @CoolMind, here is the address https://github.com/naveedahmad99/DragSortDemoAndroid – Naveed Ahmad Nov 24 '15 at 10:18