I want to realize drag and drop in my RecyclerView. As I found out, the best way to implement this is by attaching an ItemTouchHelper to the RecyclerView.
onSwiped() works fine, but onMove() is never triggered.
This is what I do:
EditIngredientsRecyclerViewAdapter premixableIngredientsAdapter = new EditIngredientsRecyclerViewAdapter(this, typeOfComponents);
GridLayoutManager gridLayoutManager1 = new GridLayoutManager(getContext(), 1);
recyclerView.setAdapter(premixableIngredientsAdapter);
recyclerView.setLayoutManager(gridLayoutManager1);
ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
Log.d(TAG, "onMove: ");
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
Log.d(TAG, "onSwiped: ");
}
};
ItemTouchHelper touchHelper = new ItemTouchHelper(simpleItemTouchCallback);
touchHelper.attachToRecyclerView(recyclerView);
I forgot two things:
**1. update the
SimpleCallbackto this:Especially important is to overwrite
getMovementFlags()to specify the direction for dragging and notify the adapter of the moved Item, as I do inonMove()2. Trigger dragging by
itemTouchHelper.startDrag(viewHolderElement):I did this by creating a small interface in the RecyclerViewAdapter:
When creating the Adapter pass in a new OnStartDragListenerElement like so:
and calling
onStartDrag(viewHolder)in an Touchlistener put on a button in the view like so:I am sure, there are more efficient implementations and once somebody posts one here, I will accept it. However, this at least made it working for me.