I Am using RecyclerView for displaying a list of items, and I need to update state for single item by position. I am updating my item in list then call notifyItemChanged(int position) like so:
myList.set(position, newModifiedItem);
notifyItemChanged(position);
The item is updated successfully but also are randomly updated some other items, and each time I scroll up and down through my item list, this updates (different icon state) is made on other items too.
I am doing changes in onBindViewHolder
where I check list item by position and decide to set different status.
Full Adapter code:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<MyObj> myList;
public MyAdapter(List<MyObj> list) {
this.myList = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_item, parent, false);
return new ViewHolder(view);
}
public void onBindViewHolder(final ViewHolder holder, final int position) {
MyObj myObj = myList.get(position);
boolean isSpecial = myObj.getMySpecialStatus();
holder.myItemTitle.setText(myObj.getTitle());
//decide if item has different icon state
if (isSpecial) {
holder.myItemIcon.setImageResource(R.drawable.ic_special);
}
}
@Override
public int getItemCount() {
return myList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView myItemTitle;
public ImageView myItemIcon;
public LinearLayout itemClickable;
//some other views
public ViewHolder(View itemView) {
super(itemView);
myItemTitle = (TextView) itemView.findViewById(R.id.item_title);
myItemIcon = (ImageView) itemView.findViewById(R.id.item_img);
itemClickable = (LinearLayout) itemView.findViewById(R.id.item_clickable_zone);
itemClickable.setOnClickListener(this);
//some other views
}
@Override
public void onClick(View v) {
final int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
switch (v.getId()) {
case R.id.item_clickable_zone: {
//some logic to change status - here just an idea
MyObj newModifiedItem = myList.get(position);
newModifiedItem.setMySpecialStatus(true);
myList.set(position, newModifiedItem);
notifyItemChanged(position);
break;
}
//some other views on click
}
}
}
}
}
The problem is in your onBindViewHolder()
where you didn't use else
case for if
statement. It shoult be like:
public void onBindViewHolder(final ViewHolder holder, final int position) {
MyObj myObj = myList.get(position);
boolean isSpecial = myObj.getMySpecialStatus();
holder.myItemTitle.setText(myObj.getTitle());
//decide if item has different icon state
if (isSpecial) {
holder.myItemIcon.setImageResource(R.drawable.ic_special);
} else {
//here is the trick - set normal state back
holder.myItemIcon.setImageResource(R.drawable.ic_standard);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With