Last active
August 22, 2018 12:21
-
-
Save theonlyanil/28602493891e7f303235dc5892c52e55 to your computer and use it in GitHub Desktop.
Swipe to Delete RecyclerView
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
// OnCreate | |
{ | |
... | |
ItemTouchHelper.SimpleCallback simpleCallback = 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) { | |
// Item swiped. | |
int pos = viewHolder.getAdapterPosition(); | |
adapter.removeItem(pos); | |
} | |
}; | |
// Attach it to recyclerview | |
new ItemTouchHelper(simpleCallback).attachToRecyclerView(recyclerView); | |
... | |
} | |
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> | |
{ | |
... /* All other code */ | |
public void removeItem(int position) | |
{ | |
datasets.remove(position); | |
notifyItemRemoved(position); | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment