Skip to content

DraggableRecyclerView provides a wrapper class DragItemTouchHelperCallback which you can use to add Dragging capability to your RecyclerView items. You can make use of DataBinding to bind it via XML.

License

Notifications You must be signed in to change notification settings

chetdeva/draggablerecyclerview

Repository files navigation

Draggable RecyclerView

DraggableRecyclerView provides a wrapper class DragItemTouchHelperCallback extends ItemTouchHelper.Callback which you can use to add Dragging capability to your RecyclerView items. You can make use of DataBinding to bind it via XML.

How to Use

ItemTouchHelper.Callback dragCallback = new DragItemTouchHelperCallback
        .Builder(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0)
        .setDragEnabled(dragEnabled)
        .onItemDragListener(onItemDrag)
        .build();

ItemTouchHelper itemTouchHelper = new ItemTouchHelper(dragCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);

How to Bind

In your Gradle

dataBinding {
    enabled = true
}

In your BindingAdapter

/**
 * @param recyclerView      RecyclerView to bind to DragItemTouchHelperCallback
 * @param dragEnabled       enable/disable swipe
 * @param onItemDrag        OnItemDragListener for dragged
 */
@android.databinding.BindingAdapter(value = {"dragEnabled", "onItemDrag"}, requireAll = false)
public static void setItemDragToRecyclerView(RecyclerView recyclerView, boolean dragEnabled,
                                             DragItemTouchHelperCallback.OnItemDragListener onItemDrag) {

    ... // attach RecyclerView to DragItemTouchHelperCallback as above
}

In your XML file

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    bind:dragEnabled="@{true}"
    bind:onItemDrag="@{(indexFrom, indexTo) -> handler.onItemDragged(indexFrom, indexTo)}"/>

Library used

Add Android Support Design dependency to your gradle file.

dependencies {
    compile 'com.android.support:design:{latest_version}'
}

Reference

About

DraggableRecyclerView provides a wrapper class DragItemTouchHelperCallback which you can use to add Dragging capability to your RecyclerView items. You can make use of DataBinding to bind it via XML.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages