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.
ItemTouchHelper.Callback dragCallback = new DragItemTouchHelperCallback
.Builder(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0)
.setDragEnabled(dragEnabled)
.onItemDragListener(onItemDrag)
.build();
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(dragCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
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)}"/>
Add Android Support Design dependency to your gradle file.
dependencies {
compile 'com.android.support:design:{latest_version}'
}