I have a RecyclerView
which is getting list of favourite and Unfavourite item. There is a Star icon in the item onClick of which i am calling an API which return added to favorite or removed from favourite. Now i am Trying to update star icon in the RecyclerView
OnSuccess
of my task i am updating my list with this code
adapterParcel.notifyItemChanged(position, modelParcelsArrayList);
adapterParcel.notifyDataSetChanged();
In my Adapter onBindViewHolder
i am trying to update view by this code
if (singleModelParcels.is_favouriteParcel()) {
itemListHolder.rpl_iv_favorite.setBackgroundResource(R.drawable.ic_action_fav_yellow);
} else {
itemListHolder.rpl_iv_favorite.setBackgroundResource(R.drawable.ic_action_fav_white);
}
I am unable to figure out what should i do to update View of that item.
The notifyDataSetChanged
method refreshes the items in the recyclerView adapter ie . they again go through the onBindViewHolder
state.
if you have modified you object properly to hold the fav value (true/false) then inside the onBindViewHolder
you can simple access that value an set the view, something like this:
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
Object object = objectList.get(position);
if (object.isFav()){
holder.favIcon.highlight();
}else{
holder.favIcon.unhighlight();
}
}
So all you have to do is call the notifyDataSetChanged method on the adapter and the code inside the onBindViewHolder
mthod will handle it for you!
Check a similar project created by me, here
您也可以通过使用notifyItemChanged实现相同的功能,但是在这种情况下,您将需要在该位置更新实体,这将刷新适配器中的视图。
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.