gpt4 book ai didi

android - 更改特殊项目颜色时出现困惑

转载 作者:行者123 更新时间:2023-11-29 18:31:07 25 4
gpt4 key购买 nike

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

我为此请求使用了以下代码,但是当项目太多时,我向下滚动

所有其他项目的背景颜色也发生变化

这种情况在正常的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);
}
}

}

最佳答案

您做对了,但由于 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);
}
}

关于android - 更改特殊项目颜色时出现困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56234416/

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