I have successfully implemented the swipe to delete action within my recyclerView.
The list I inflate the recyclerView with is locally stored and isn't in any database.
What I tried to do is to save the element to a temporary variable and if the user presses the undo button, I would restore it.
This is my code:
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val position = viewHolder.adapterPosition //get position which is swipe
if (direction == ItemTouchHelper.LEFT) { //if swipe left
val tmp = activeSubs[position] //temporary variable
activeSubs.removeAt(position)
adapter!!.removeItem(position)
val layout = find<View>(R.id.active_subs_recycler)
Snackbar.make(layout, "Subscription Deleted", Snackbar.LENGTH_LONG)
.setAction("Undo", { _ ->
activeSubs.add(position-1, tmp)
adapter!!.notifyDataSetChanged()
})
}
}
My problem is that the SnackBar isn't shown at all, but the element is saved in the temporary variable and deleted from the list.
What am I doing wrong?
P.S. I've looked into other answers here on SO but they suggested to use a Dialog. I really need to use a Snackbar though