RecyclerView ItemTouchHelper Action Drag Ended

Sebastian picture Sebastian · May 25, 2016 · Viewed 7.5k times · Source

I need to listen to the user when he stops drag and drop on my RecyclerView (when he drops the selected item).

Can I get this Information through my ItemTouchHelper?

Thanks for helping

Mark: At the moment i only now when the user still moves the item :)

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
    if (source.getItemViewType() != target.getItemViewType()) {
        return false;
    }
    // Notify the adapter of the move
    mAdapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition());
    return true;
}

Answer

cyroxis picture cyroxis · May 25, 2016

UPDATED

First you can define where an object can be dropped by implementing canDropOver

@Override
public boolean canDropOver(RecyclerView recyclerView, RecyclerView.ViewHolder current, RecyclerView.ViewHolder target) {
    return current.getItemViewType() == getItemViewType();
}

Update your adapter you want to use onMove this can be called multiple during a drag operation

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
    adapter.moveItem(viewHolder.getAdapterPosition(), target.getAdapterPosition());
    return true;
}

To detect when an interaction with an element is over implement clearView this is for any action type (drag or swipe) in the even that it succeeds (item moved or swiped) or is canceled (item not moved or swiped)

@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    super.clearView(recyclerView, viewHolder);
    // Action finished
}