question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RecyclerView Delete Element on Swipe

See original GitHub issue

I’ve noticed some strange issues with Swiping to Delete elements. To make this easy, I’ve used the sample to show case my issue. If I swipe to delete an element, the element below stays in the delete state. Basically, it looks like the bottom layout stays over the next element.

Here’s what happens in the sample: Original Load: screenshot_2015-08-10-12-25-39

Swipe Left on Top Element (Alabama):

screenshot_2015-08-10-12-26-23

Notice that Alabama gets deleted, but Alaska (the element below) still has the bottom view open. I can’t figure out how to prevent this behavior. To try out the code, simply replace the OnCreateViewHolder from the sample with the following (has some logging):

    @Override
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item, parent, false);
    final SimpleViewHolder viewHolder = new SimpleViewHolder(view);

    SwipeLayout swipeLayout = viewHolder.swipeLayout;

    swipeLayout.addSwipeListener(new SimpleSwipeListener() {

        @Override
        public void onOpen(SwipeLayout layout) {

            Log.d("Swiped Opened", viewHolder.textViewData.getText().toString());
            int position = mDataset.indexOf(viewHolder.textViewData.getText());
            mItemManger.removeShownLayouts(viewHolder.swipeLayout);
            mDataset.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position, mDataset.size());
            mItemManger.closeAllItems();

            Log.d("Swipe Deleted", viewHolder.textViewData.getText().toString() + String.valueOf(position));
            //Toast.makeText(view.getContext(), "Deleted " + viewHolder.textViewName.getText().toString() + "!", Toast.LENGTH_SHORT).show();

        }

    });

    return viewHolder;
}

I noticed when I remove the notifyItemRangeChanged(…), the situation looks a little better, but it turns out random elements still look like Delete Item. Any thoughts/suggestions?

Edit: Ignore the multiple/single mode. Looks like this happens regardless 😦

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
Alexander--commented, Aug 12, 2015

This is because SwipeItemMangerImpl tracks item state using position in list instead of id. Position is just a visible location on screen, so when something gets deleted (and another item takes it’s position), things get ugly.

Usage of stable ids (returned by adapter.getItemId(position)) would solve this issue. You can read more about stables ids in this SO answer; it is about ListView, but same things apply to RecyclerView too.

Hope this helps. I also have a branch with fix, but it is against 1.1.8, and has few other changes.

0reactions
r020477888commented, Nov 5, 2020

Same problem…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android RecyclerView Swipe To Delete And Undo
Swipe to delete feature is commonly used to delete rows from a RecyclerView. In order to implement Swipe to delete feature, we need...
Read more >
Step by Step: RecyclerView Swipe to Delete and Undo - Medium
Step 1: ItemTouchHelper.SimpleCallback · Step 2: Attach ItemTouchHelper to the Recyclerview · Step 3: Deleting the item · Step 4: Undo Snackbar.
Read more >
Android - Swipe to delete RecyclerView - Stack Overflow
ViewHolder viewHolder, int swipeDir) { //Remove swiped item from list and notify the RecyclerView mAdapter.notifyItemRemoved(viewHolder.
Read more >
Swipe to Delete and Undo in Android RecyclerView
Swipe to Delete and Undo in Android RecyclerView · Step 1: Create a New Project · Step 2: Create a Card Layout for...
Read more >
Android swipe menu with RecyclerView | by Artur Grzybowski
Very simple solution to create swipe menu with RecyclerView without any additional libraries — using ItemTouchHelper Callback for swiping items and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found