更改特殊项目颜色时出现混乱

时间:2019-05-21 08:42:15

标签: android recycler-adapter

我想在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);
    }
}

}

3 个答案:

答案 0 :(得分:2)

您确实纠正了,但由于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);
}
}

答案 1 :(得分:0)

您要做的是将'[_]'放在以下条件之后的代码中,这应该没问题。

else-statement

答案 2 :(得分:0)

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