android - Changing drawable in RecyclerView updates all rows -
i trying 'like' button functionality in application facebook. want whenever user clicks on button, color of drawable on button should change grey blue. using recyclerview feeds. since, recyclerview enforces use viewholder pattern, color of buttons changed thereafter. appreciate solve me problem.
this code adapter
public class facebooktimelineadapter : recyclerview.adapter { context mcontext { get; set;} list<facebookmodel> mdata; layoutinflater inflater; facebookviewholder holder; public facebooktimelineadapter (context context,list<facebookmodel> data) { this.mcontext = context; this.mdata = data; this.inflater = layoutinflater.from (context); } #region implemented abstract members of adapter public override void onbindviewholder (recyclerview.viewholder viewholder, int position) { holder = viewholder facebookviewholder; holder.username.text = mdata [position].username; holder.timestamp.text = mdata [position].timestamp; if(!(mdata[position].profilepictureid==0) || !(mdata[position].profilepictureid==null)) holder.profilepicture.setimagedrawable (mcontext.resources.getdrawable(mdata[position].profilepictureid)); if (mdata [position].feedmessage == "") { holder.feedmessage.visibility = viewstates.gone; } else { holder.feedmessage.visibility = viewstates.visible; holder.feedmessage.text = mdata [position].feedmessage; } if ((mdata [position].feedurl == "") || (mdata [position].feedurl ==null)) { holder.feedurl.visibility = viewstates.gone; } else { holder.feedurl.visibility = viewstates.visible; holder.feedurl.text = mdata [position].feedurl; } if ((mdata [position].feedimage==null) || (mdata [position].feedimage==0)) { holder.feedimage .visibility = viewstates.gone; } else { holder.feedimage .visibility = viewstates.visible; holder.feedimage.setimagedrawable(mcontext.resources.getdrawable(mdata[position].feedimage)) ; } holder.like.click += delegate { drawable img = mcontext.resources.getdrawable (resource.drawable.fb_comment); holder.like.setcompounddrawableswithintrinsicbounds (img, null, null, null); }; } public override recyclerview.viewholder oncreateviewholder (viewgroup parent, int viewtype) { return new facebookviewholder (inflater.inflate (resource.layout.timeline_item_facebook,parent,false),this); } public override int itemcount { { return mdata.count; } } public void likeclicked(int position) { drawable img = mcontext.resources.getdrawable (resource.drawable.fb_comment); holder.like.setcompounddrawableswithintrinsicbounds (img, null, null, null); } #endregion } }
this code viewholder
public class facebookviewholder : recyclerview.viewholder { public textview username,timestamp,feedurl,feedmessage; public imageview profilepicture,feedimage; public button like, comment; public facebookviewholder(view item, facebooktimelineadapter adapter) : base(item) { username=item.findviewbyid<textview>(resource.id.fb_username); timestamp=item.findviewbyid<textview>(resource.id.fb_time); feedurl=item.findviewbyid<textview>(resource.id.fb_feed_url); feedmessage=item.findviewbyid<textview>(resource.id.fb_feed_message); profilepicture=item.findviewbyid<imageview>(resource.id.fb_profilepicture); feedimage=item.findviewbyid<imageview>(resource.id.fb_feed_image); like=item.findviewbyid<button>(resource.id.fb_feed_like); comment=item.findviewbyid<button>(resource.id.fb_feed_comment); } }
code in c# since developing android app in xamarin.
when using recycle view pattern should 'reset' state of recycled view initial state, way not have changes made 1 child applied other when view got recycled.
Comments
Post a Comment