Created
July 13, 2018 16:20
-
-
Save thanhit93/954435928f511dff3f51753a9065d90f to your computer and use it in GitHub Desktop.
swipe-to-dismiss-for-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
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
https://stackoverflow.com/questions/30713121/disable-swipe-for-position-in-recyclerview-using-itemtouchhelper-simplecallback