I am working on a messaging application and would like to set the chat bubble as coming from the left or the right depending on who the owner of the message is.
public class Message {
public String messageText;
public boolean mine;
// ...Constructor
}
Inside MessageAdapter I have:
@Override
public MessageHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(this.itemResource, parent, false);
return new MessageHolder(this.context, view);
}
@Override
public void onBindViewHolder(MessageHolder holder, int position) {
Message message = this.messages.get(position);
holder.bindMessage(message);
}
I have been following tutorials to migrate from ListView to RecyclerView and am now unclear as to how to access both an item's data and its view at the same time without getting NullPointerException. The code I want to use looks like this:
Drawable background = getResources().getDrawable(message.mine ? R.drawable.bubble_textra_right : R.drawable.bubble_textra_left);
background.setColorFilter(getResources().getColor(R.color.ColorPrimary), PorterDuff.Mode.SRC_IN);
view.findViewById(R.id.message_text).setBackgroundDrawable(background);
However, if I put it in onCreateViewHolder I can't figure out how to access the individual message to check ownership. If I put it in onBindViewHolder I have message but can't get its view. How do I solve this?
Aucun commentaire:
Enregistrer un commentaire