I have been battling with RecyclerView for the last 2 days. Unlike any time in the past, I want to have swipe to dismiss functionality.
I followed this guide
The only difference here that I have implemented is instead of using a predefined array for input, I have an API response to populate the data. All that seems to be good, when I open the recyclerview the items populate as they should. In the order that they should.
Here is where the fun begins. If I try to swipe to dismiss the first item I get an IndexOutOfBounds error. Thats cute, searching I found that some choose to set a new List based on the first list, remove the item and then re-assign the original list to the new list. notifyDataSetChanged() when done. No more error BUT no animation of the recycler view to bring the 2nd item to the top of the list. any time notifyDataSetChanged() is called the list does NOT move. I scroll down and back up and the item is back. Or so I thought.
After playing with this for a while I noticed that while the information in the first item appeared to the be the original first item, its got an ID of the second item in the list and the last item in the view is now missing.
Here is an example:
I swipe dismiss 1 from the list, this is what the UI shows
Lots of code to embed here and its throwing errors trying to do so, gist is here https://gist.github.com/baggednismo/2840d0f777438d29673bc29096153970
@Scott Stanchfield was very much correct on his input. The noted changes in his comments and answer cleaned up the view to help me find the actual view related issue. The suggestions are still implemented and are part of the solution.
onBindViewHolder() created the view of items of which it was creating the line items based on the original API response order and not the list order recyclerview was handling. onBindViewHolder() is called each time a new item is to appear in the list on scroll and this was the problem. This works fine on the very first inflation of the list however once the item was removed and scrolling back to the top the first item appeared to be the item dismissed.
Original:
KitchenOrdersResponse.Item mItem = mKitchenOrdersResponse.getItems().get(i);
Resolution:
KitchenOrdersResponse.Item mItem = mItems.get(i);