Last active
August 29, 2015 14:25
-
-
Save atoennis/18e6bd530cf9e1dcbc96 to your computer and use it in GitHub Desktop.
How to implement swipe to delete with a Recyclerview using the support library. Functionality was added in version 22.2 of the support library.
This file contains hidden or 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
compile 'com.android.support:recyclerview-v7:22.2.0' |
This file contains hidden or 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
recyclerView.setHasFixedSize(true); | |
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); | |
recyclerView.setAdapter(adapter); | |
SimpleCallback simpleCallback = new SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) { | |
@Override | |
public boolean onMove(RecyclerView recyclerView, ViewHolder viewHolder, ViewHolder viewHolder1) { | |
return false; | |
} | |
@Override | |
public void onSwiped(ViewHolder viewHolder, int i) { | |
int pos = viewHolder.getAdapterPosition(); | |
listener.onItemSwiped(items.get(pos)); // Tell the listener about the swipe so it can perform the delete. | |
} | |
}; | |
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback); | |
itemTouchHelper.attachToRecyclerView(recyclerView); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment