当前位置: 首页 > 知识库问答 >
问题:

改变特殊物品颜色时弄乱

穆毅然
2023-03-14

我想在recyclerview中更改一个特殊项目的颜色

我使用了下面的代码来处理这个请求,但是当项目太多时,我会向下滚动

所有其他项目的背景颜色也会改变

此案例属于正常的回收视图

我该怎么解决这个问题?

我的适配器代码

public class LastCommentAdapter extends RecyclerView.Adapter<LastCommentAdapter.LastCommentViewHolder> {
private Context context;
private List<Comment> comments;
View view;

public LastCommentAdapter(Context context, List<Comment> comments) {
    this.context = context;
    this.comments = comments;
}

@Override
public LastCommentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    view = LayoutInflater.from(context).inflate(R.layout.layout_last_comment_item, parent, false);
    return new LastCommentViewHolder(view);
}

@Override
public void onBindViewHolder(final LastCommentViewHolder holder, int position) {
    final Comment comment = comments.get(position);
    holder.commentUser.setText(comment.getName_user());
    holder.commentContent.setText(comment.getContent());
    holder.numberSubComment.setText(String.valueOf(comment.getNumber_sub_comment()));
    if (comment.getId()==7) {
        holder.itemView.setBackgroundColor(Color.BLUE);
    }
}

@Override
public int getItemCount() {
    return comments.size();
}

public class LastCommentViewHolder extends RecyclerView.ViewHolder {
    private TextView commentContent;
    private TextView commentUser;
    private TextView numberSubComment;

    public LastCommentViewHolder(View itemView) {
        super(itemView);
        commentContent = (TextView) itemView.findViewById(R.id.last_comment_item_content);
        commentUser = (TextView) itemView.findViewById(R.id.last_comment_item_user);
        numberSubComment = (TextView) itemView.findViewById(R.id.last_comment_item_answer);
    }
}

}

共有2个答案

宰父志新
2023-03-14
Your should change your code like below

    @Override
    public void onBindViewHolder(final LastCommentViewHolder holder, int position) {
        final Comment comment = comments.get(position);
        holder.itemView.setBackgroundColor(Color.YourDefaultColorOFItem);
        if(comment != null){
           holder.commentUser.setText(comment.getName_user());
           holder.commentContent.setText(comment.getContent());
        holder.numberSubComment.setText(String.valueOf(comment.getNumber_sub_comment()));
          if (comment.getId()==7) {
             holder.itemView.setBackgroundColor(Color.BLUE);
          } else holder.itemView.setBackgroundColor(Color.YourDefaultColorOFItem);
       }
    }
丌官嘉良
2023-03-14

确实正确,但由于Id更改,还需要为else语句添加代码。问题是,特殊项目将发生变化,由于else声明,它不会再次获得白色背景。

@Override
public void onBindViewHolder(final LastCommentViewHolder holder, int position) {
final Comment comment = comments.get(position);
holder.commentUser.setText(comment.getName_user());
holder.commentContent.setText(comment.getContent());
holder.numberSubComment.setText(String.valueOf(comment.getNumber_sub_comment()));
if (comment.getId()==(Special Item Id)) {
    holder.itemView.setBackgroundColor(Color.BLUE);
}else{
    holder.itemView.setBackgroundColor(Color.YOUR_COLOR);
}
}
 类似资料:
  • 只是尝试在CSS中练习悬停和下拉。在下面的代码中,我希望每当下拉子div(带有Home1文本的绿色div)悬停在上面时,的背景色(红色)应该更改为蓝色。 会很感激你的帮助。 null null

  • 我使用的是Eclipse Color Theme插件。 当我更改主题时,除了.等之外,所有的颜色都改变了。 它们保持黑色,而当我试图使用深色主题时,这使得事情变得困难。 问题是在使用Groovy时没有正确设置语法颜色。 这解决了问题

  • 我已经想了几个小时了,但运气不好。 现在,WooCommerce特色产品小部件垂直显示产品。我想让特色产品并排(水平)显示。 我找遍了所有我能想到的地方,但我找不到我需要编辑的东西来实现这一点。 所有的帮助将不胜感激!

  • 我的目标的在线示例: 我试图在分类页面上显示自行车产品。每个产品上都有一种不同的边框颜色,我用圆形div来显示。 当我点击一种颜色时,图像应该会发生变化,正如你在专业网站上看到的那样:(产品图像下方)专业示例。 我自己的网站 您可以在这里看到我自己的示例:我自己的网站示例(向下滚动,直到您看到具有多个产品的产品)。 我的目标功能: 当我单击绿色div时,我想显示图片2,当我单击红色div时,我想看

  • 我是Java脚本的初学者,我用Java脚本编写了bellow代码,将文本颜色变为红色,但它不起作用,我的代码中有什么错误吗?

  • 我有一个房子的资源链接,如果你想查看它。它基本上只是一个黑色的主页按钮。 我有它包括和导入作为图像资产作为绘图。 我试图将其设置为按钮,但以编程方式将其更改为白色。 以下是我尝试将颜色更改为白色的方法: 知道我哪里出错了吗?