更改RedrawerView中的drawable更新所有行

更改RedrawerView中的drawable更新所有行,第1张

概述我试图像在Facebook上一样在我的应用程序中获得“赞”按钮功能.我想要的是,每当用户单击“喜欢”按钮时,按钮上可绘制对象的颜色应从灰色变为蓝色.我正在将RecyclerView用于提要.由于RecyclerView强制使用了Viewholder模式,因此所有类似按钮的颜色此后都会更改.我真的很感谢能为我

我试图像在Facebook上一样在我的应用程序中获得“赞”按钮功能.我想要的是,每当用户单击“喜欢”按钮时,按钮上可绘制对象的颜色应从灰色变为蓝色.我正在将RecyclerVIEw用于提要.由于RecyclerVIEw强制使用了VIEwholder模式,因此所有类似按钮的颜色此后都会更改.
我真的很感谢能为我解决这个问题的一些帮助.

这是适配器的代码

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 as 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 {        get {            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}}

这是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);        }    }

因为我在Xamarin中开发androID应用,所以代码在C#中.

解决方法:

使用回收视图模式时,应将回收视图的状态“重置”为其初始状态,这样,在回收视图时,您将不会对一个子对象所做的更改应用于其他子对象.

总结

以上是内存溢出为你收集整理的更改RedrawerView中的drawable更新所有行全部内容,希望文章能够帮你解决更改RedrawerView中的drawable更新所有行所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1089619.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存