Bauerca drag-sort-listview simple example

AntounG picture AntounG · Feb 11, 2013 · Viewed 20.8k times · Source

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

Answer

Lescai Ionel picture Lescai Ionel · Feb 22, 2013

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>