For more detail article please kindly read : Swipe for delete, edit and share item on Recyclerview
In this tutorial, i still use THIS PROJECT, so i just post the code that related with swipe function.
There is no much steps you will do, just write a several line codes in your MainActivity.java or wherever you define your Recyclerview. In this case, I define the Recyler view in MainActivity.java
For Undo function, we will use on the the newest materials called Snackbar. Snackbar will appears at the bottom when you delete the item.
For use Snackbar, make sure you have compiled this library in your build.gradle(Module:app) :
compile 'com.android.support:design:25.0.1
After that, in you class where you define the recyclerview, paste the code below :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//swipe for delete item | |
ItemTouchHelper touchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) { | |
@Override | |
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { | |
return false; | |
} | |
@Override | |
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { | |
final int position = viewHolder.getAdapterPosition(); | |
String name = mListUser.get(position).getName(); | |
mUserAdapter.notifyItemRemoved(position); | |
Snackbar.make(viewHolder.itemView, name + " was removed", Snackbar.LENGTH_LONG).setAction("UNDO", new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
mUserAdapter.notifyItemInserted(position); | |
mUserAdapter.notifyDataSetChanged(); | |
} | |
}).show(); | |
} | |
}); | |
touchHelper.attachToRecyclerView(rvUser); |
EmoticonEmoticon