I have a RecyclerView that list data from server and it must show each row of data differently according to conditions. My data is list of a ticket messages. if a message is sent from the user, it's background must be colored gray and if it's from an agent it must be blue. in addition if this messages has an attachment it must show an download icon. here is my Adaptor code :
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
if (holder.getItemViewType() == 0) {
holder.itemView.setBackgroundResource(R.drawable.radius_background_gray);
holder.ticketDate.setBackgroundResource(R.drawable.radius_background_light_gray);
}
if(ticket.get(position).getAttachment() != null)
holder.ticketStatus.setImageResource(R.drawable.ic_attachment_black_24dp);
holder.ticketTitle.setText(ticket.get(position).getAgentName() + " said :");
}
@Override
public int getItemViewType(int position) {
if(ticket.get(position).getDirection().equals("out"))
return 0;
else return 1;
}
colors are being set correctly but when I use if clause to set an imageView for attachment a message that has no attachment and is null shows that attachment icon! Should I also check this condition in getItemType and not in onBindViewHolder?