在RecyclerView中更改drawable会更新所有行

时间:2015-06-18 20:29:03

标签: android android-recyclerview android-viewholder

我正试图在我的应用程序中获得'喜欢'按钮功能,就像Facebook一样。我想要的是,每当用户点击相似按钮时,按钮上的可绘制颜色应该从灰色变为蓝色。我正在使用RecyclerView作为提要。由于RecyclerView强制使用视图模式,因此之后会更改所有相似按钮的颜色。 我真的很感激帮助解决这个问题。

这是适配器的代码

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);

        }

    }

代码在C#中,因为我正在开发Xamarin中的Android应用程序。

2 个答案:

答案 0 :(得分:3)

我有点解决了Marcos提供的解决方案。

这是我的适配器

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

  // Made changes in data structure to see if the post was liked or not. 

    if (mData [position].liked) {
            Drawable img = mContext.Resources.GetDrawable (Resource.Drawable.fb_liked);
            holder.like.SetCompoundDrawablesWithIntrinsicBounds (img, null, null, null);
        } else {
            Drawable img = mContext.Resources.GetDrawable (Resource.Drawable.fb_like);
            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;
        }
    }

 // On Clicking the like button notifyItemChanged() is called
    public void LikeClicked(int position)
    {
        if (mData [position].liked)
            mData [position].liked = false;
        else
            mData [position].liked = true;

        NotifyItemChanged (position);

    }

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

           // Added Click listener in Viewholder
            like.Click += delegate {
                adapter.LikeClicked(LayoutPosition);
            }; 
        }

    }

答案 1 :(得分:1)

使用回收视图模式时,您应该重置&#39;将循环视图的状态恢复到其初始状态,这样,当视图被回收时,您不会对一个子应用于其他子进行更改。