[JAVA] Sort ArrayList by dragging and dropping ItemTouchHelper

ItemTouchHelper used in RecyclerView can easily implement drag & drop and swipe.

However, is it possible to change the order of list data internally with Drag & Drop? So, even if you save the list, it will be restored at the next startup. So I added a little code.

It's simple.

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target)
{
    final int fromPosition = viewHolder.getAdapterPosition();
    final int toPosition = target.getAdapterPosition();
    recyclerAdapter.notifyItemMoved(fromPosition, toPosition);
    //Add from here
    String temp = arrayList.get(fromPosition);
    arrayList.remove(fromPosition);
    arrayList.add(toPosition, temp);
    //So far
    return true;
}

The point is to add it after erasing it.

Recommended Posts

Sort ArrayList by dragging and dropping ItemTouchHelper
LinkedList and ArrayList
[Sort] Sort by following a user, following a follower, and following (descending order)!