RecyclerView ItemTouchHelper Action Drag Ended
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
}
Easy solution using onSelectedChanged. Using drag flag reacts only on drag and not swipe.
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
boolean drag = false;
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
// YOUR onMove
return false;
}
@Override
public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
super.onSelectedChanged(viewHolder, actionState);
if(actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
drag = true;
Log.d("DragTest","DRAGGGING start");
}
if(actionState == ItemTouchHelper.ACTION_STATE_IDLE && drag) {
Log.d("DragTest","DRAGGGING stop");
drag= false;
}
}
}).attachToRecyclerView(mRecyclerView);
You can override onSelectedChange in your implementation of ItemTouchHelper.Callback(), such as:
override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
super.onSelectedChanged(viewHolder, actionState)
when (actionState) {
ItemTouchHelper.ACTION_STATE_DRAG ->
Log.d("DragTest","Start to drag: $actionState")
ItemTouchHelper.ACTION_STATE_SWIPE ->
Log.d("DragTest","Start to swipe: $actionState")
ItemTouchHelper.ACTION_STATE_IDLE -> {
Log.d("DragTest","End action: $actionState")
}
}
}