Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Swipe to delete RecyclerView

I am trying to implement swipe to delete the same as Gmail app "Swipe to archive":

gmail swipe delete gmail swipe delete

I have tried many tutorials but none of them works as fast as gmail, I prefer to not work on external library. How can i do it?

Edit:

My code so far-

  ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {                 public boolean onMove(RecyclerView recyclerView,                                                RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { //                    final int fromPos = viewHolder.getAdapterPosition(); //                    final int toPos = viewHolder.getAdapterPosition(); //                    // move item in `fromPos` to `toPos` in adapter.                     return true;// true if moved, false otherwise                 }              @Override             public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {                 //Remove swiped item from list and notify the RecyclerView                 mAdapter.notifyItemRemoved(viewHolder.getLayoutPosition());             }         };         ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);         itemTouchHelper.attachToRecyclerView(mRecyclerView); 

I've managed to enable swipe gesture but i don't know how to add a background and an image under the viewHolder. I tried to put another FrameLayout at the item_XXX.xml file but on swipe it throws the whole item with the background.

like image 403
Montoya Avatar asked Nov 29 '15 17:11

Montoya


People also ask

What is RecyclerView ViewHolder?

A RecyclerView. ViewHolder class which caches views associated with the default Preference layouts. A ViewHolder describes an item view and metadata about its place within the RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive findViewById results.


2 Answers

Simple Code for RecyclerView Swipe:

     ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.DOWN | ItemTouchHelper.UP) {              @Override             public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {                 Toast.makeText(ListActivity.this, "on Move", Toast.LENGTH_SHORT).show();                 return false;             }              @Override             public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {                 Toast.makeText(ListActivity.this, "on Swiped ", Toast.LENGTH_SHORT).show();                 //Remove swiped item from list and notify the RecyclerView                 int position = viewHolder.getAdapterPosition();                 arrayList.remove(position);                 adapter.notifyDataSetChanged();              }      }; 

Then set callback for recyclerView with below statements:

ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);  itemTouchHelper.attachToRecyclerView(rv); 
like image 78
Rahul Raina Avatar answered Sep 27 '22 21:09

Rahul Raina


I had to do this the other day and I had some issues so I decided to write a blog post on it. No 3rd party lib necessary.

  • Blog post
  • GitHub repo

Basically, you wouldn't draw the "undo state" via onChildDraw, it would be done via ViewHolder. Also you wouldn't actually delete row in onSwipe just mark it as "pending delete" and notify adapter to rebind it in "undo state". At the same time you post a Runnable actually removing the row in x seconds unless undo button is pressed...

like image 25
Nemanja Kovacevic Avatar answered Sep 27 '22 20:09

Nemanja Kovacevic