Android-ItemTouchHelper-Demo icon indicating copy to clipboard operation
Android-ItemTouchHelper-Demo copied to clipboard

getAdapterPosition() can be wrong

Open scriptacus opened this issue 10 years ago • 2 comments

This isn't a bug with this code, but I'm throwing it out there as a potential gotcha that I only ran into after implementing the dismiss/swap functionality from this demo.

After a dismiss or swap of items in a recycler view, "holder.getAdapterPosition()" will return NO_POSITION (-1) if you've called "notifyDataSetChanged()" since the last layout pass. I was passing getAdapterPosition() to my Activity in response to some user interaction, which had always worked previously. After either of the actions above, the adapter position became invalid until an orientation change or attempt to scroll the screen. Using getLayoutPosition() in place of getAdapterPosition() resolved the issue.

scriptacus avatar Aug 06 '15 23:08 scriptacus

Thanks for info.

Farasy avatar Sep 11 '15 21:09 Farasy

Yes I noticed this, my app start crashes sometimes on swipe; when I run the test getAdapterPosition is -1. :( is there a fix for this.

pixelbendr avatar Sep 25 '15 15:09 pixelbendr