Skip to content

Instantly share code, notes, and snippets.

@thanhit93
Created July 13, 2018 16:20
Show Gist options
  • Save thanhit93/954435928f511dff3f51753a9065d90f to your computer and use it in GitHub Desktop.
Save thanhit93/954435928f511dff3f51753a9065d90f to your computer and use it in GitHub Desktop.
swipe-to-dismiss-for-recyclerview
As of v27.0, the Android support team has included an ItemTouchHelper class that makes swipe-to-dismiss and drag-and-drop pretty simple. This may not be as full-featured as some of the libraries out there, but it comes directly from the Android team.
Update your build.gradle to import v27.+ of the RecyclerView library
compile 'com.android.support:recyclerview-v7:27.+'
Instantiate an ItemTouchHelper with an appropriate SimpleCallback
ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
[...]
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
//Remove swiped item from list and notify the RecyclerView
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
** Note that the SimpleCallback takes in the directions that you want to enable drag-and-drop and the directions that you want to enable swiping.
Attach to your RecyclerView
itemTouchHelper.attachToRecyclerView(recyclerView);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment