java – Is it possible to drag an item outside of RecyclerView?-ThrowExceptions

Exception or error:

I need to somehow notify RecyclerView when I drag and drop an item from another RecyclerView onto it.

Drag and drop an item to another RecyclerView

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.

How to solve:

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 classid 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:

  1. Create the drag shadow when e.g. long pressing an item in RecyclerView 1 (it’s a copy of the item, drawn over the app)
  2. The shadow can be moved around, handled by the framework already; this will call drag enter/exits and drag events on the views it is draggede over
  3. add a 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

Leave a Reply

Your email address will not be published. Required fields are marked *