I need to somehow notify RecyclerView
when I drag and drop an item from another RecyclerView
onto it.
RecyclerView
with blue items is in one fragment and RecyclerView
with red items is in another fragment.
I also tried using ItemTouchHelper but it's onMove() method from ItemTouchHelper.Callback is not called while moving with item outside
from RecyclerView
.
private class CustomItemTouchCallback extends Callback {
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
return makeMovementFlags(UP|DOWN|START|END, 0);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
android.util.Log.d(TAG, "Move item from:" + viewHolder.getAdapterPosition() + " to: " + target.getAdapterPosition());
return true;
}
@Override
public void onMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int fromPos, RecyclerView.ViewHolder target, int toPos, int x, int y) {
android.util.Log.d(TAG, "Moved item from:" + fromPos + " to: " + toPos + " x: " + x + " y: " + y);
super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
}
@Override
public boolean isLongPressDragEnabled() {
return true;
}
@Override
public boolean isItemViewSwipeEnabled() {
return false;
}
}
I also find this question, but it doesn't solve my problem.
Android Swipe To Delete. Swipe to delete feature is commonly used to delete rows from a RecyclerView. In order to implement Swipe to delete feature, we need to use the ItemTouchHelper utility class.
Drag and Drop can be added in a RecyclerView using the ItemTouchHelper utility class. Following are the important methods in the ItemTouchHelper. Callback interface which needs to be implemented: isLongPressDragEnabled - return true here to enable long press on the RecyclerView rows for drag and drop.
Old question, but no answer yet, so I provide a short one...
For dragging something from one view to another you can use the DragShadowBuilder
. The ItemTouchHelper
is only meant for moving items within the RecyclerView
, you need a custom implementation for what you want.
Check out this documentation: https://developer.android.com/guide/topics/ui/drag-drop (so yes, for what you want you should is this classic drag&drop framework as you called it)
It shows you how to create the shadow, how to move it (done automatically) and how to react to the drag event in the second view.
The workflow is following:
RecyclerView
1 (it's a copy of the item, drawn over the app)View.OnDragListener
on the second RecyclerView
and handle the events (e.g. add the data in the second RecyclerView
if the user drops it over it and remove it from the first RecyclerView
)With the help of DragShadowBuilder
you can find tutorials on this, like e.g. the one here: http://www.vogella.com/tutorials/AndroidDragAndDrop/article.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With