其他分享
首页 > 其他分享> > 更改RedrawerView中的drawable更新所有行

更改RedrawerView中的drawable更新所有行

作者:互联网

我试图像在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#中.

解决方法:

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

标签:android-viewholder,android-recyclerview,android
来源: https://codeday.me/bug/20191028/1950533.html