Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
**BETA** Android 4.0-style "Swipe to Dismiss" sample code
Moved to
https://github.com/romannurik/android-swipetodismiss
@afollestad

This comment has been minimized.

Show comment Hide comment
@afollestad

afollestad Jun 26, 2012

This is very useful, thanks!

This is very useful, thanks!

@ameryzhu

This comment has been minimized.

Show comment Hide comment
@ameryzhu

ameryzhu Feb 17, 2013

Pretty good, but requires higher api level 10 and 11.It would be better if it can run on a 2.2 machine.

Pretty good, but requires higher api level 10 and 11.It would be better if it can run on a 2.2 machine.

@mhd-adeeb-masoud

This comment has been minimized.

Show comment Hide comment
@mhd-adeeb-masoud

mhd-adeeb-masoud Feb 23, 2013

Thank you so much

Thank you so much

@JoeyZhao317

This comment has been minimized.

Show comment Hide comment
@JoeyZhao317

JoeyZhao317 Mar 12, 2013

There is a big bug in SwipeDismissListViewTouchListener.java. If user swipe multiple items in a short time, you can find some items view will not be really dismissed, but, the item will be deleted from list...

There is a big bug in SwipeDismissListViewTouchListener.java. If user swipe multiple items in a short time, you can find some items view will not be really dismissed, but, the item will be deleted from list...

@JoeyZhao317

This comment has been minimized.

Show comment Hide comment
@JoeyZhao317

JoeyZhao317 Mar 12, 2013

And, I think you should add ACTION_CANCEL event:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
//.....
unless, there will be a bug when you hold a view to swipe then rotate the screen of phone, the view will stay where they have been moved and cannot dismiss or be back to the origin position where it was.

And, I think you should add ACTION_CANCEL event:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
//.....
unless, there will be a bug when you hold a view to swipe then rotate the screen of phone, the view will stay where they have been moved and cannot dismiss or be back to the origin position where it was.

@aramiku

This comment has been minimized.

Show comment Hide comment
@aramiku

aramiku Jul 7, 2013

Sometimes when you swipe up just to roll the list in the intent to show the itens below, if the swipe go a litle bit aside, the item you put your finger on is deleted. The gesture is interpreted wrong . I sugest a more longer swipe to the side to be interpreted as a swipe to delete/dismiss. Great Job.

aramiku commented Jul 7, 2013

Sometimes when you swipe up just to roll the list in the intent to show the itens below, if the swipe go a litle bit aside, the item you put your finger on is deleted. The gesture is interpreted wrong . I sugest a more longer swipe to the side to be interpreted as a swipe to delete/dismiss. Great Job.

@pjbrinck

This comment has been minimized.

Show comment Hide comment
@pjbrinck

pjbrinck Jul 9, 2013

Roman,

Really nice work...

I am experiencing, when also adding an OnItemLongClickListener to the ListView, that SwipeDismissListViewTouchListener is not paused during long clicks. This would be similar to pausing SwipeDismissListViewTouchListener during scrolling.

Would you consider updating SwipeDismissListViewTouchListener to also manage such?

best regards

pjbrinck commented Jul 9, 2013

Roman,

Really nice work...

I am experiencing, when also adding an OnItemLongClickListener to the ListView, that SwipeDismissListViewTouchListener is not paused during long clicks. This would be similar to pausing SwipeDismissListViewTouchListener during scrolling.

Would you consider updating SwipeDismissListViewTouchListener to also manage such?

best regards

@MattSkala

This comment has been minimized.

Show comment Hide comment
@MattSkala

MattSkala Jul 9, 2013

Thanks, awesome!
And with use of http://nineoldandroids.com/, you can get it working on older versions.

Thanks, awesome!
And with use of http://nineoldandroids.com/, you can get it working on older versions.

@pjbrinck

This comment has been minimized.

Show comment Hide comment
@pjbrinck

pjbrinck Jul 19, 2013

Roman,

I am working my way thru the code to identify the cause of a "bug"... but having some difficulty getting my head around it. I was hoping we could do this together.

I'm experiencing, when in onItemClick() I call adapter.notifyDataSetChanged(), adapter.getView() is NOT called for the item/position clicked, but is nicely called for all other visible items/positions.

If I omit setting the SwipeDismissListViewTouchListener on the list view (disabling the swipe-remove functionality), adapter.getView() is CERTAINLY called for all visible items, including the clicked item, in the scenario above.

Please help me identify what causes this slightly different behavior.

best regards pjbrinck

Roman,

I am working my way thru the code to identify the cause of a "bug"... but having some difficulty getting my head around it. I was hoping we could do this together.

I'm experiencing, when in onItemClick() I call adapter.notifyDataSetChanged(), adapter.getView() is NOT called for the item/position clicked, but is nicely called for all other visible items/positions.

If I omit setting the SwipeDismissListViewTouchListener on the list view (disabling the swipe-remove functionality), adapter.getView() is CERTAINLY called for all visible items, including the clicked item, in the scenario above.

Please help me identify what causes this slightly different behavior.

best regards pjbrinck

@jesdga95

This comment has been minimized.

Show comment Hide comment
@jesdga95

jesdga95 Jul 22, 2013

@pjbrinck, you probably already fixed it, but I faced this bug today, and fixed it (plus another small bug):

https://gist.github.com/D4rKn3sSyS/6057805

Roman, thank you very much for this snippet, very useful for today's Android UI design.

@pjbrinck, you probably already fixed it, but I faced this bug today, and fixed it (plus another small bug):

https://gist.github.com/D4rKn3sSyS/6057805

Roman, thank you very much for this snippet, very useful for today's Android UI design.

@tasomaniac

This comment has been minimized.

Show comment Hide comment
@tasomaniac

tasomaniac Aug 23, 2013

I have a problem with CursorLoaders. When you use this with a regular ArrayAdapter, you can remove all the dismissed items and then call notifyDataSetChanged() w/o any problem. When I use SimpleCursorAdapter with CursorLoader and LoaderManager, it automatically detects the changes in the database and update the UI whenever a change occurs. So the result it that there are items going out and coming again and going out again. How can I solve this? Has anyone experienced such problem?

I have a problem with CursorLoaders. When you use this with a regular ArrayAdapter, you can remove all the dismissed items and then call notifyDataSetChanged() w/o any problem. When I use SimpleCursorAdapter with CursorLoader and LoaderManager, it automatically detects the changes in the database and update the UI whenever a change occurs. So the result it that there are items going out and coming again and going out again. How can I solve this? Has anyone experienced such problem?

@zagayevskiy

This comment has been minimized.

Show comment Hide comment
@zagayevskiy

zagayevskiy Oct 1, 2013

Hello! It's very good job. I have a small quastion: How can I disable swipe to one side (for example swipe to left)?

Hello! It's very good job. I have a small quastion: How can I disable swipe to one side (for example swipe to left)?

@suzhouliuyan

This comment has been minimized.

Show comment Hide comment
@suzhouliuyan

suzhouliuyan Nov 12, 2013

It is very good job.

It is very good job.

@vnguyen972

This comment has been minimized.

Show comment Hide comment
@vnguyen972

vnguyen972 Dec 6, 2013

So I was trying to use this little library for my list and when I put a list inside a PagerViewer... the swipe won't work, everytime I tried, it just swipe between the pages on the PagerViewer, is there a way to distinguish between them? can you swipe the list item horizontally without the PagerViewer intercepts the swipe and swipe its pages?

So I was trying to use this little library for my list and when I put a list inside a PagerViewer... the swipe won't work, everytime I tried, it just swipe between the pages on the PagerViewer, is there a way to distinguish between them? can you swipe the list item horizontally without the PagerViewer intercepts the swipe and swipe its pages?

@Yougin

This comment has been minimized.

Show comment Hide comment
@Yougin

Yougin Apr 17, 2014

Has anyone found a solution for a problem tasomaniac mentioned (SwipeToDismiss and CursorLoader) ?

Yougin commented Apr 17, 2014

Has anyone found a solution for a problem tasomaniac mentioned (SwipeToDismiss and CursorLoader) ?

@tasomaniac

This comment has been minimized.

Show comment Hide comment
@tasomaniac

tasomaniac Apr 18, 2014

@Yougin I haven't tried but this can solve it. You can disable automatic loading of the CursorLoader using a custom CursorLoader. Chris Banes does it in his open-source Photup application. https://github.com/chrisbanes/photup/blob/master/client/src/uk/co/senab/photup/util/PhotupCursorLoader.java

@Yougin I haven't tried but this can solve it. You can disable automatic loading of the CursorLoader using a custom CursorLoader. Chris Banes does it in his open-source Photup application. https://github.com/chrisbanes/photup/blob/master/client/src/uk/co/senab/photup/util/PhotupCursorLoader.java

@cornwe19

This comment has been minimized.

Show comment Hide comment
@cornwe19

cornwe19 Aug 15, 2014

You might want to make a temporary cursor that manually hides the dismissed indices while the updated cursor loads. Something like this https://gist.github.com/cornwe19/6be23fde488c577691d3 might accomplish what you need.

You might want to make a temporary cursor that manually hides the dismissed indices while the updated cursor loads. Something like this https://gist.github.com/cornwe19/6be23fde488c577691d3 might accomplish what you need.

@bishnu371

This comment has been minimized.

Show comment Hide comment
@bishnu371

bishnu371 Oct 31, 2014

Very good library , i am using it.
Please help me ,How to handle left and right swipe?

Thanks In Advance

Very good library , i am using it.
Please help me ,How to handle left and right swipe?

Thanks In Advance

@GeekerAndyLove

This comment has been minimized.

Show comment Hide comment
@GeekerAndyLove

GeekerAndyLove Jul 6, 2017

Thanks a lot.

Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment