Skip to content

Instantly share code, notes, and snippets.

@walidum
Created April 25, 2021 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walidum/52a200b9f8801b56599f676ce686bbc8 to your computer and use it in GitHub Desktop.
Save walidum/52a200b9f8801b56599f676ce686bbc8 to your computer and use it in GitHub Desktop.
Swipe to Delete and Undo in Android RecyclerView
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
NotificationData.Result deletedCourse = notificationList.get(viewHolder.getAdapterPosition());
int position = viewHolder.getAdapterPosition();
notificationList.remove(viewHolder.getAdapterPosition());
recyclerAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
Snackbar.make(binding.recyclerView, deletedCourse.title, Snackbar.LENGTH_LONG).setAction("Annuler", new View.OnClickListener() {
@Override
public void onClick(View v) {
notificationList.add(position, deletedCourse);
recyclerAdapter.notifyItemInserted(position);
}
}).show();
}
}).attachToRecyclerView(binding.recyclerView);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment