public class MessageListAdapter extends RecyclerView.Adapter {
private static final int VIEW_TYPE_MESSAGE_SENT = 1;
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
private Context mContext;
private List<UserMessage> mMessageList;
public MessageListAdapter(Context context, List<UserMessage> messageList) {
mContext = context;
mMessageList = messageList;
@Override
public int getItemCount() {
return mMessageList.size();
// Determines the appropriate ViewType according to the sender of the message.
@Override
public int getItemViewType(int position) {
UserMessage message = (UserMessage) mMessageList.get(position);
if (message.getSender().getUserId().equals(currentUserId)) {
// If the current user is the sender of the message
return VIEW_TYPE_MESSAGE_SENT;
} else {
// If some other user sent the message
return VIEW_TYPE_MESSAGE_RECEIVED;
// Inflates the appropriate layout according to the ViewType
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if (viewType == VIEW_TYPE_MESSAGE_SENT) {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_message_sent, parent, false);
return new SentMessageHolder(view);
} else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED) {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_message_received, parent, false);
return new ReceivedMessageHolder(view);
return null;
// Passes the message object to a ViewHolder so that the contents can be bound to UI.
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
UserMessage message = (UserMessage) mMessageList.get(position);
switch (holder.getItemViewType()) {
case VIEW_TYPE_MESSAGE_SENT:
((SentMessageHolder) holder).bind(message);
break;
case VIEW_TYPE_MESSAGE_RECEIVED:
((ReceivedMessageHolder) holder).bind(message);
private class SentMessageHolder extends RecyclerView.ViewHolder {
SentMessageHolder(View itemView) {
super(itemView);
//bind views
void bind(UserMessage message) {
//set values
private class ReceivedMessageHolder extends RecyclerView.ViewHolder {
ReceivedMessageHolder(View itemView) {
super(itemView);
//bind views