Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to drag an item outside of RecyclerView?

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

  • Is it possible?
  • Or should I use classic Drag and drop framework?

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.

like image 526
martaus Avatar asked Jun 06 '16 13:06

martaus


People also ask

How do I move items in RecyclerView?

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.

How do I drag and drop items in RecyclerView Android?

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.


1 Answers

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

like image 190
prom85 Avatar answered Oct 26 '22 05:10

prom85