DECISION - HOPE THAT SOME POSSIBLE THIS WILL HELP
I found a soution for this. If someone tries to dynamically remove elements from their list of arrays and notifyItemRemoved(position)not send the click position as a parameter inside onBindViewHolder(ViewHolder holder, int position). You will face the same situation as me.
4 f.e. [0, 1, 2, 3] , . clicked positions positions in ArrayList. , 4- :
position = 3 - , ; myArray.remove(position) - index = 3 notifyItemRemoved(position) - . : [0, 1, 2]. .
, . , . , , :
position = 2 → myArray.remove(position) → notifyItemRemoved(position)
ArrayList, , : [0, 1, 3]. , :
position = 3 → myArray.remove(position) → notifyItemRemoved(position)
? : java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3. , , . ? ... :
At the beggining we had:
- → [0, 1, 2, 3]
POSITIONS FROM CLICK → [0, 1, 2, 3]
After Deleting 3rd element:
- → [0, 1, 2]
POSITIONS FROM CLICK → [0, 1, 3]
, position = 3, . . , , 2. . ?
onBindViewHolder(ViewHolder holder, int position) position
removeFromFavourites(position). holder. , : getAdapterPosition() RecyclerView.ViewHolder, .
getAdapterPosition
: http://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder.html#getAdapterPosition()
, ArrayList. , , , position holder.getAdapterPosition():
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
RecipeItem item = items.get(position);
Picasso.with(context).load(item.getImgThumbnailLink()).into(holder.recipeItemImage);
holder.recipeItemTitle.setText(item.getTitle());
String subtitle = "Kuchnia " + item.getKitchenType() + ", " + item.getMealType();
holder.recipeItemSubtitle.setText(subtitle);
holder.recipeItemLikesCount.setText(Integer.toString(item.getLikeCount()));
holder.recipeItemAddDate.setText(item.getAddDate());
holder.recipeItemOptionsIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(context, v);
setPopUpListener(popupMenu, holder.getAdapterPosition());
inflatePopupMenu(popupMenu);
popupMenu.show();
}
});
holder.setClickListener(new RecipeItemClickListener() {
@Override
public void onClick(View view, int position) {
}
});
}