Swipe to delete in a list using RecyclerView

Viewed 3574

I am trying to get the swipe-to-delete feature in my recyclerview. I am using onSwiped() method of ItemTouchHelper.SimpleCallback.
I get the swipe animation, and the swiped row collapses, when I use adapter.notifyItemRemoved() method. I checked the row data gets deleted from database. But, the deleted row again is regenerated and displayed on the recyclerview at the same position.

  @Override
  public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {

         ListOfItemsRecyclerViewCursorAdapter adapter = (ListOfItemsRecyclerViewCursorAdapter) rv.getAdapter();

         int position = viewHolder.getAdapterPosition();
         long itemIdToBeRemoved = rv.getAdapter().getItemId(position);

         //Deletes the item from sqlite database
         mDBManager.deleteItem(itemIdToBeRemoved);      

         adapter.notifyItemRemoved(position);

         // Using notifyItemRangeChanged, the swiped row does not collapse, but stays as a blank row.
         adapter.notifyItemRangeChanged(position, adapter.getItemCount());

         //adapter.notifyDataSetChanged();
         //Tried the following - but did not work
         //getLoaderManager().initLoader(1, null, ListOfItemsFragment.this);

        }

Seeing a few other SO questions, I tried to use adapter.notifyItemRangeChanged() method, but that stops the collapsing animation of the row, and just leaves a blank row. Blank row on swipe

I am using a Loader to load the cursor for the adapter from SQLite database. So I also tried to call initloader() method, but that also did not work.

Could you please help me solve this problem. I have tried searching for the solution on Stack Overflow but have not been able to solve the problem.

Thanks

3 Answers
Related