I used to SwipeToDismiss library but now I'm trying to migrate to RecyclerView and things are not so obvious, do you know any replacements for this lib? Any ideas how to implement it from the scratch?
-
2I have made small library which use ItemTouchHelper to make gestures creation for recyclerview easier, you can find it here github.com/olmur/rvtools – Olexii Muraviov Feb 08 '17 at 11:54
5 Answers
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);

- 4,463
- 2
- 16
- 20
-
7
-
39@Orochi By calling [getAdapterPosition()](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder.html#getAdapterPosition%28%29) on the `viewHolder`. – SqueezyMo Jul 21 '15 at 12:13
-
Hi guys! And how do I reach the getAdapterPosition() from the Activity? The viewHolder is on my Adapter and I don't know how to get the View position that I'm swipping from the ItemTouchHelper. Did I must to do that on Adapter's holder? How? PS: Feel free to ask to move this as a new question. – Martin Revert Jul 30 '15 at 15:21
-
Nevermind, I found the viewHolder reference when the onSwipe() is called. Thank you!. – Martin Revert Jul 30 '15 at 15:57
-
-
1They clearly didn't put too much thought into the design of this component. It only works with a RecyclerView. Swipe-to-dismiss exists for things like snackbars. A more generic component that could be used with any view would have been more welcomed. – Johann Nov 27 '15 at 16:22
-
4What if I want to handle the case when the user swipe partially but then drag the view back in position? Apparently this is not possibile(?) EDIT: ok, it IS possibile, but there's a very little margin which you can stay in before the view get swiped on release. Any suggestion? – Lampione Jan 18 '16 at 19:35
-
2@Matteo: Implement ItemTouchHelper.Callback and override getSwipeThreshold() – Sofi Software LLC Mar 11 '16 at 17:51
-
1For me the following blog post with provided example application on git was very helpful. I recommend you to go through it. It is demonstrating implementation of swiping items in RecyclerView also with the Undo functionality and background colored hints. [RecyclerView “swipe to delete”. No 3rd party lib necessary](http://nemanjakovacevic.net/blog/english/2016/01/12/recyclerview-swipe-to-delete-no-3rd-party-lib-necessary/) – RenatoIvancic Apr 29 '16 at 10:29
-
I have made small library which use ItemTouchHelper to make gestures creation easier, you can find it here github.com/olmur/rvtools – Olexii Muraviov Feb 08 '17 at 11:55
-
I try out you solution but I have a crash :https://stackoverflow.com/questions/45098895/noclassdeffounderror-android-support-v7-widget-helper-itemtouchhelper3 – ovluca Jul 14 '17 at 11:18
ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction) {
final int position = viewHolder.getAdapterPosition(); //get position which is swipe
if (direction == ItemTouchHelper.LEFT) { //if swipe left
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); //alert for confirm to delete
builder.setMessage("Are you sure to delete?"); //set message
builder.setPositiveButton("REMOVE", new DialogInterface.OnClickListener() { //when click on DELETE
@Override
public void onClick(DialogInterface dialog, int which) {
adapter.notifyItemRemoved(position); //item removed from recylcerview
sqldatabase.execSQL("delete from " + TABLE_NAME + " where _id='" + (position + 1) + "'"); //query for delete
list.remove(position); //then remove item
return;
}
}).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { //not removing items if cancel is done
@Override
public void onClick(DialogInterface dialog, int which) {
adapter.notifyItemRemoved(position + 1); //notifies the RecyclerView Adapter that data in adapter has been removed at a particular position.
adapter.notifyItemRangeChanged(position, adapter.getItemCount()); //notifies the RecyclerView Adapter that positions of element in adapter has been changed from position(removed element index to end of list), please update it.
return;
}
}).show(); //show alert dialog
}
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
itemTouchHelper.attachToRecyclerView(recyclerView); //set swipe to recylcerview
Here in Code if user swipe left then AlertDialog is displayed and if user select REMOVE then item is deleted from database and recyclerview is refreshed and if user select CANCEL then recyclerview is as it is.

- 1,042
- 13
- 22
-
-
-
-
adapter.notifyItemRemoved(position + 1); will get back item that is swiped if user select CANCEL (means don't want to delete item) – Khyati Vara Aug 25 '17 at 06:03
-
1You don't really need the direction check `if (direction == ItemTouchHelper.LEFT) // if swipe left` as the `ItemTouchHelper.SimpleCallback` is limited to just that swipe direction. If you want left and right swipes then `ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT)` and then you would need to check the direction. – Jacko Oct 22 '17 at 23:17
-
1I found that clicking outside the AlertDialog canceled the dialog but did not put the item I swiped back. You can capture this adding an OnCancelListener to the Builder `AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);builder.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { // stuff to put the item back } });` – Jacko Oct 22 '17 at 23:19
-
-
1+1 Works well. I found `adapter.notifyItemChanged(position);` brought the swiped item back, rather than `notifyItemRemoved` - which is more logical imho. – winwaed Jul 26 '18 at 20:03
-
-
works exactly same as i want. you can also include delete api call in alert dialog positive button click.. nice – Ajay Mistry Feb 13 '19 at 07:22
-
This answer is helpful for laying out how to make it happen, but there is a HUGE problem with it. Determining the ID in your database by the position on your list is a mistake. Why? For one, once you delete a single row, it no longer works. This all assumes all the IDs are exactly matched up to the rows in the first place. It also doesn't work if your data is sorted by some other field or moved around or really anything. Just want to make sure people realize that when reading this answer. It's kind of a big deal. I did leverage this answer to set mine up correctly though. – Benjamin May 01 '20 at 09:24
-
working fine but when i tried to delete last item and click the cancel button that(last item of list ) is gone – Arbaz.in Aug 25 '20 at 09:55
maybe you could try this library:
https://github.com/daimajia/AndroidSwipeLayout
Update: I've just found another good library that you can use with RecyclerView:

- 231
- 2
- 14
-
I made my own implementation similar to https://github.com/krossovochkin/Android-SwipeToDismiss-RecyclerView. The only requirement was to show Toast with "Undo" button, wich is not covered in your lib. – Viktor Yakunin Feb 27 '15 at 10:10
-
1The library from Daimajia does not support the swipe-to-dismiss feature. – akohout Jun 23 '15 at 08:04
-
This library may be helpful.You can implement undo
in OnDissmiss
use supertoast

- 1,121
- 9
- 17
-
Hey, I'm going to give it a try! Is it based on the one answered by Pierpaolo? – Boy May 05 '15 at 12:23
-
It's just a `OnTouchListener` inspire by [this](https://github.com/romannurik/Android-SwipeToDismiss) – xcodebuild May 06 '15 at 12:56
I wrote SwipeToDeleteRV library which supports swipe-to-delete-undo feature on recycler views. It is based on ItemTouchHelper and very easy to use.
Hope it may be helpful for someone facing the same issues.
As an example, you can define your recycler view in an XML layout as normal, plus some optional attributes:
...
xmlns:stdrv="http://schemas.android.com/apk/res-auto"
...
<io.huannguyen.swipetodeleterv.STDRecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
stdrv:border_color="@android:color/darker_gray" // specify things like border color, border width, etc.
stdrv:delete_view_background="#cccccc"
stdrv:delete_icon="@drawable/ic_archive"
stdrv:delete_icon_height="24dp"
stdrv:delete_icon_width="24dp"
stdrv:left_delete_icon_margin="32dp"
stdrv:delete_message="@string/delete_message"
stdrv:right_delete_icon_margin="32dp"
stdrv:delete_icon_color="#000000"
stdrv:has_border="true"/>
All stdrv attributes are optional. If you don't specify them, the default ones would be used.
Then create an adapter that subclasses STDAdapter, make sure you call the super class constructor. Something like this:
public class SampleAdapter extends STDAdapter<String> {
public SampleAdapter(List<String> versionList) {
super(versionList);
}
}
Next make sure you make a call to the setupSwipeToDelete
method to set the swipe-to-delete feature up.
mRecyclerView.setupSwipeToDelete(your_adapter_instance, swipe_directions);
swipe_directions
is the direction you allow items to be swiped.
Example:
// Get your recycler view from the XML layout
mRecyclerView = (STDRecyclerView) findViewById(R.id.recycler_view);
LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(layoutManager);
mAdapter = new SampleAdapter(versions);
// allow swiping in both directions (left-to-right and right-to-left)
mRecyclerView.setupSwipeToDelete(mAdapter, ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT);
That's it! For more advanced settings (i.e., set different deletion messages for different items, temporarily and permanently remove items,...) please refer to the project readme page.

- 1,621
- 5
- 19
- 31