24

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.

timiTao
  • 1,417
  • 3
  • 20
  • 34
martaus
  • 312
  • 3
  • 8
  • 1
    @maratus i wanted to use the same functionality did u find any solution to it? – Arslan Sohail Dec 15 '17 at 11:37
  • What did you end up doing? – Martin De Simone Mar 11 '18 at 18:55
  • Since RecyclerView provides the `OnItemTouchListener` to handle touch events, what you would need to achieve in theory, is to create an identical item to the new recycler view and remove it from the previous one, by coding for the desired motion event. It's probably not exactly what you want, but it's the same functionalitiy in principle. – Mitsakos Mar 15 '18 at 10:51
  • i am also stuck in that ,you got your solution? – Nikunj Paradva Apr 16 '18 at 09:59
  • did you check [this](https://stackoverflow.com/questions/29690522/drag-and-drop-between-two-recyclerview), it's an old question, but seems to be the same problem! – user1 May 06 '18 at 09:52
  • Possible duplicate of [Drag and Drop between two RecyclerView](https://stackoverflow.com/questions/29690522/drag-and-drop-between-two-recyclerview) – Hitesh Sahu May 07 '18 at 07:10

1 Answers1

3

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:

  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

Sayem
  • 4,891
  • 3
  • 28
  • 43
prom85
  • 16,896
  • 17
  • 122
  • 242