DragSortRecycler icon indicating copy to clipboard operation
DragSortRecycler copied to clipboard

getItemId in sample code should not use position

Open snooplsm opened this issue 10 years ago • 4 comments

The getItemId in the sample code should not return position. If items are added or removed it will break the RecyclerView.

snooplsm avatar Dec 29 '14 20:12 snooplsm

Ah thanks for the info, can you explain this a bit more please? Under what circumstances will it break, you mean if you do a notifyItemRemoved for example? TBH I wasn't entirely sure what should be returned here. Thanks.

emileb avatar Dec 29 '14 20:12 emileb

View the last comment here: https://code.google.com/p/android/issues/detail?id=77846

It should be the primary key from a database, and I guess you can get away with hashCode if your items are unique.

snooplsm avatar Dec 29 '14 20:12 snooplsm

OK thanks for the link. So it appears if you (wrongly) return position in getItemId you must use the generic notifyDataSetChanged and not the other notify* functions, otherwise it may confuse the recycler. I will look into this limitation. Thank you.

emileb avatar Dec 29 '14 21:12 emileb

Just need to keep track of the position. Once my local changes are done I'll create a patch.

moveInterface.onItemMoved(selectedDragPosition, (int) newPos);

snooplsm avatar Dec 29 '14 21:12 snooplsm