I've a function like this one:
private void addItem(final int position, Meals newMeal){
if(isLeft(position)){
Meals.add(position+2, newMeal);
}
else{
Meals.add(position+1, newMeal);
}
notifyItemInserted(position);
}
Where I want to add a selected item (duplicate it) +2 positions (starting from current one) if it's left item (I've two columns) or +1 if it's right one. Right now, it just inserts items in wrong positions. Any ideas what's wrong?
Maybe try the following:
private void addItem(final int position, Meals newMeal){
if(isLeft(position)){
Meals.add(position+2, newMeal);
notifyItemInserted(position+2);
}
else{
Meals.add(position+1, newMeal);
notifyItemInserted(position+1);
}
}
According to RecyclerView.Adapter/#notifyItemInserted :
Notify any registered observers that the item reflected at position has been newly inserted. The item previously at position is now at position position + 1.
Hope it helps
use .set() method instead of add
For adding items, just append them to Meal and then go notifyDatasetChanged(). Another way to go is to use notifyItemRangeInserted(). Depending on how are you adding new items and how many of them, it might be worth it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.