gpt4 book ai didi

android - 在 RecyclerView 中更改可绘制对象会更新所有行

转载 作者:太空宇宙 更新时间:2023-11-03 13:19:20 25 4
gpt4 key购买 nike

我正在尝试在我的应用程序中获得“赞”按钮功能,就像在 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);

}

}

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

最佳答案

我在某种程度上解决了 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);
};
}

}

关于android - 在 RecyclerView 中更改可绘制对象会更新所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30925236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com