Android 更改特殊项目颜色时出现混乱
我想更改recyclerview中特殊项目的颜色 我使用了下面的代码来处理这个请求,但是当项目太多时,我会向下滚动 所有其他项目的背景色也会改变 此案例处于正常的recyclerview中 我该怎么解决这个问题 我的适配器代码Android 更改特殊项目颜色时出现混乱,android,android-recyclerview,Android,Android Recyclerview,我想更改recyclerview中特殊项目的颜色 我使用了下面的代码来处理这个请求,但是当项目太多时,我会向下滚动 所有其他项目的背景色也会改变 此案例处于正常的recyclerview中 我该怎么解决这个问题 我的适配器代码 public class LastCommentAdapter extends RecyclerView.Adapter<LastCommentAdapter.LastCommentViewHolder> { private Context context;
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);
}
}
}
public类LastCommentAdapter扩展了RecyclerView.Adapter{
私人语境;
私人名单评论;
视图;
公共LastCommentAdapter(上下文、列表注释){
this.context=上下文;
this.comments=注释;
}
@凌驾
public LastCommentViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图=LayoutFlater.from(上下文)。充气(R.layout.layout\u last\u comment\u item,parent,false);
返回新的LastCommentViewHolder(视图);
}
@凌驾
public void onBindViewHolder(最终LastCommentViewHolder,int位置){
最终评论=comments.get(位置);
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(颜色:蓝色);
}
}
@凌驾
public int getItemCount(){
返回注释。size();
}
公共类LastCommentViewHolder扩展了RecyclerView.ViewHolder{
私有文本查看评论内容;
私有文本视图用户;
私有文本视图编号subcomment;
公共LastCommentViewHolder(查看项目视图){
超级(项目视图);
commentContent=(TextView)itemView.findViewById(R.id.last\u comment\u item\u content);
commentUser=(TextView)itemView.findViewById(R.id.last\u comment\u item\u user);
numberSubComment=(TextView)itemView.findviewbyd(R.id.last\u comment\u item\u response);
}
}
}
您确实更正了,但由于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);
}
}
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);
}
}