As of v22.2.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 v22.2.+ of the RecyclerView library
compile 'com.android.support:recyclerview-v7:22.2.+'
-
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);