androidandroid-recyclerviewitemtouchhelper

How to know when user is done moving a RecyclerView item?


I need to execute something when the user drops an item. The ItemTouchHelper seems to only have calls for onMove and onMoved which happen regardless of whether the user is actually done moving the item. How do I know when the user has finished moving an item? Ie, they have released their finger and dropped the item back into the recyclerView?


Solution

  • Easy. Just override onClearView():

    @Override
    public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {    //called when you dropped the item
        super.clearView(recyclerView, viewHolder);
    
        Toast.makeText(recyclerView.getContext(), "Item dropped on position: " + viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
    }