{"id":31609,"date":"2016-05-24T10:49:17","date_gmt":"2016-05-24T05:19:17","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=31609"},"modified":"2016-05-25T11:46:40","modified_gmt":"2016-05-25T06:16:40","slug":"swipe-to-dismiss-recyclerview","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/swipe-to-dismiss-recyclerview\/","title":{"rendered":"Swipe To Dismiss RecyclerView"},"content":{"rendered":"<p>This tutorial is about how to create \u201cswipe to dismiss\u201d RecyclerView item like Gmail app. For this, we don&#8217;t need any 3rd party Library. For this purpose, you just need to add one class which is part of Android Support Library. The name is \u201cItemTouchHelper\u201d.<\/p>\n<p>By now you know that this blog is all about how to use ItemTouchHelper class to make your <a href=\"http:\/\/www.tothenew.com\/blog\/recyclerview-in-android\/\">RecyclerView<\/a> item swipeable.<\/p>\n<p>Let&#8217;s first understand ItemTouchHelper class. If you really don&#8217;t bother about all the theory like me then go to this link to clone the code and start experimenting.<br \/>\n<a href=\"http:\/\/github.com\/bharatghimire005\/Swipeable-RecyclerView.git\">Swipe To Dismiss RecyclerView<\/a><\/p>\n<h3>ItemTouchHelper Class<\/h3>\n<p>ItemTouchHelper is a utility class to add swipe to dismiss and drag &amp; drop support to RecyclerView. ItemTouchHelper class extends <a href=\"http:\/\/developer.android.com\/reference\/android\/support\/v7\/widget\/RecyclerView.ItemDecoration.html\"><span style=\"color: #000000;\">RecyclerView.ItemDecoration<\/span><\/a><b> <\/b><span style=\"color: #000000;\">and Implements<\/span><b> <\/b><a href=\"http:\/\/developer.android.com\/reference\/android\/support\/v7\/widget\/RecyclerView.OnChildAttachStateChangeListener.html\"><span style=\"color: #000000;\">RecyclerView.OnChildAttachStateChangeListener<\/span><\/a><b> <\/b><span style=\"color: #000000;\">. In<\/span><span style=\"color: #000000;\"> Order to use ItemTouchHelper, you need to create <\/span><span style=\"color: #000000;\"><b>ItemTouchHelper.Callback<\/b><\/span><span style=\"color: #000000;\">. <\/span><span style=\"color: #000000;\">This class let<\/span><span style=\"color: #000000;\">s<\/span><span style=\"color: #000000;\"> you control the touch event like swipe and move by providing a callback for the same. It also provides the callback to override the default animation.<\/span><\/p>\n<p><span style=\"color: #000000;\">To implement Swipe to Dismiss behaviour in recycler view we have to override following methods of ItemTouchHelper.Callback.<\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\">getMovementFlags(RecyclerView recyclerView,ViewHolder viewHolder)<\/span><\/li>\n<li><span style=\"color: #000000;\">onMove(RecyclerVIew recyclerView,ViewHolder viewHolder,ViewHolder viewHolderTarget) (needed if you want to add functionality of drag and drop).<\/span><\/li>\n<li><span style=\"color: #000000;\">onSwiped(ViewHolder, int)<\/span><\/li>\n<\/ul>\n<p><span style=\"color: #000000;\">Before implementing ItemTouch Helper lets first go with these callbacks. The First method we are going to discuss is <\/span><\/p>\n<p><span style=\"color: #000000;\"><b>getMovementFlags(RecyclerView ,ViewHolder)<\/b><\/span><br \/>\n<span style=\"color: #000000;\">It takes parameters<\/span><\/p>\n<ul>\n<li><strong>recyclerView<\/strong>: The RecyclerView to which ItemTouchHelper is attached.<\/li>\n<li><strong>viewHolder<\/strong>: The ViewHolder for which the movement information is necessary.<\/li>\n<\/ul>\n<p>and returns<\/p>\n<ul>\n<li>flags specifying which movements are allowed on this ViewHolder.<\/li>\n<\/ul>\n<pre class=\"western\"><span style=\"color: #000000;\">@Override<\/span>\r\npublic int <strong>getMovementFlags<\/strong>(RecyclerView recyclerView, \r\n        RecyclerView<strong>.<\/strong>ViewHolder viewHolder) {\r\n    int swipeFlags = Direction.RIGHT;\r\n    return makeMovementFlags(0, swipeFlags);\r\n}<\/pre>\n<p><span style=\"color: #000000;\">To<\/span><span style=\"color: #000000;\"> specify in which direction you want to swipe, you have to override this method. In our example, <\/span><span style=\"color: #000000;\">we enable only swipe right. makeMovementFlag(int, int) provide convenience to create Movement flag.<\/span><\/p>\n<p><span style=\"color: #000000;\">The second method we are going to discuss is<\/span><\/p>\n<h4><span style=\"color: #000000;\">onMove<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">RecyclerView<\/span><span style=\"color: #000000;\"> recyclerView, <\/span><span style=\"color: #000000;\">RecyclerView.ViewHolder<\/span><span style=\"color: #000000;\"> viewHolder, <\/span><span style=\"color: #000000;\">RecyclerView.ViewHolder<\/span><span style=\"color: #000000;\"> target)<\/span><\/h4>\n<p class=\"western\"><span style=\"color: #000000;\">It takes parameters<\/span><\/p>\n<ul>\n<li class=\"western\"><strong>recyclerView<\/strong>: The RecyclerView to which ItemTouchHelper is attached to.<\/li>\n<li class=\"western\"><strong>viewHolder<\/strong>: The ViewHolder which is being dragged by the user.<\/li>\n<li class=\"western\"><strong>target<\/strong>:\u00a0 The ViewHolder over which the currently active item is being dragged.<\/li>\n<\/ul>\n<p>and returns<\/p>\n<ul>\n<li>true if the viewHolder has been moved to the adapter position of the target.<\/li>\n<\/ul>\n<p><span style=\"color: #000000;\">If you want to make any changes during dragging the RecyclerView item then you have to override this method. <\/span><span style=\"color: #000000;\">If this method returns true, ItemTouchHelper assumes item<\/span><code><\/code><span style=\"color: #000000;\"> has been moved to the adapter position of target place<\/span><code class=\"western\"><span style=\"color: #000000;\">.<\/span><\/code><br \/>\n<span style=\"color: #000000;\">The Final and important method for the swipe to dismiss is<\/span><\/p>\n<h4>onSwiped(RecyclerView.ViewHolder viewHolder, int direction)<\/h4>\n<p><span style=\"color: #000000;\">It takes parameters<\/span><\/p>\n<ul>\n<li><strong>viewHolder<\/strong>: The ViewHolder which has been swiped by the user.<\/li>\n<li><strong>direction: <\/strong>The direction to which the ViewHolder is swiped. It is one of, UPDOWN, LEFT or RIGHT. If your getMovementFlags(RecyclerView, ViewHolder) method returned relative flags instead of LEFT; RIGHT direction will be relative as well. (START or END).<\/li>\n<\/ul>\n<p><span style=\"color: #000000;\">This method is called when user swiped the item. <\/span>In our example, we just delete that item which is swiped right.<\/p>\n<pre class=\"western\"><span style=\"color: #000000;\">@Override<\/span>\r\n<span style=\"color: #000000;\">public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {<\/span>\r\n<span style=\"color: #000000;\">itemTouchHelperAdapter.swipeToDelete(viewHolder.getAdapterPosition());}<\/span><\/pre>\n<p><span style=\"color: #000000;\">let&#8217;s discuss the code in the GitHub<\/span><\/p>\n<p><span style=\"color: #000000;\">We create a helper class called ItemSwipeHelper which extends ItemTocuhHelper.Callback where we override all the callback method<\/span><\/p>\n<pre class=\"western\">public class ItemSwipeHelper extends ItemTouchHelper.Callback {\r\n   private int direction;\r\n   private ItemTouchHelperAdapter itemTouchHelperAdapter;\r\n    public ItemSwipeHelper(int direction,ItemTouchHelperAdapter itemTouchHelperAdapter) {\r\n        this.direction = direction;\r\n        this.itemTouchHelperAdapter= itemTouchHelperAdapter;\r\n    }\r\n    @Override\r\n    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {\r\n       int swipeDirection=0;\r\n        switch(direction)\r\n        {\r\n            case Direction.LEFT:\r\n                swipeDirection=ItemTouchHelper.LEFT;\r\n            break;\r\n            case Direction.RIGHT:\r\n                swipeDirection=ItemTouchHelper.RIGHT;\r\n                break;\r\n            case Direction.UP:\r\n                swipeDirection=ItemTouchHelper.UP;\r\n                break;\r\n            case Direction.DOWN:\r\n                swipeDirection=ItemTouchHelper.DOWN;\r\n                break;\r\n        }\r\n        return makeMovementFlags(0,swipeDirection);\r\n    }\r\n    @Override\r\n    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {\r\n        return false;\r\n    }\r\n    @Override\r\n    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {\r\n        itemTouchHelperAdapter.swipeToDelete(viewHolder.getAdapterPosition());\r\n    }\r\n}\r\n<\/pre>\n<p><span style=\"color: #000000;\">Now our callbacks are ready, we can create our ItemTouchHelper and call attachToRecyclerView(RecyclerView)<\/span><\/p>\n<pre class=\"western\">ItemTouchHelper.Callback itemTouchCallBack=new ItemSwipeHelper(Direction.RIGHT,movieAdapter);\r\nItemTouchHelper swipeToDismissTouchHelper =new ItemTouchHelper(itemTouchCallBack);\r\nswipeToDismissTouchHelper.attachToRecyclerView(mRecyclerview);\r\n<\/pre>\n<p><span style=\"color: #000000;\">That&#8217;s all for now, we are ready to go. For any queries please comment.<\/span><\/p>\n<p><span style=\"color: #000000;\">Thanks for reading. Happy learning!<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial is about how to create \u201cswipe to dismiss\u201d RecyclerView item like Gmail app. For this, we don&#8217;t need any 3rd party Library. For this purpose, you just need to add one class which is part of Android Support Library. The name is \u201cItemTouchHelper\u201d. By now you know that this blog is all about [&hellip;]<\/p>\n","protected":false},"author":344,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":19},"categories":[518,446,1772],"tags":[3385,4844,3384,3383,3386],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/31609"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/344"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=31609"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/31609\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=31609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=31609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=31609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}