Last active
August 6, 2018 04:54
-
-
Save alkjez/551eabf861b2fd8cc929dc4c34f52f10 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<BaseMessage> mMessageList; | |
public MessageListAdapter(Context context, List<BaseMessage> 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(SendBird.getCurrentUser().getUserId())) { | |
// 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 { | |
TextView messageText, timeText; | |
SentMessageHolder(View itemView) { | |
super(itemView); | |
messageText = (TextView) itemView.findViewById(R.id.text_message_body); | |
timeText = (TextView) itemView.findViewById(R.id.text_message_time); | |
} | |
void bind(UserMessage message) { | |
messageText.setText(message.getMessage()); | |
// Format the stored timestamp into a readable String using method. | |
timeText.setText(Utils.formatDateTime(message.getCreatedAt())); | |
} | |
} | |
private class ReceivedMessageHolder extends RecyclerView.ViewHolder { | |
TextView messageText, timeText, nameText; | |
ImageView profileImage; | |
ReceivedMessageHolder(View itemView) { | |
super(itemView); | |
messageText = (TextView) itemView.findViewById(R.id.text_message_body); | |
timeText = (TextView) itemView.findViewById(R.id.text_message_time); | |
nameText = (TextView) itemView.findViewById(R.id.text_message_name); | |
profileImage = (ImageView) itemView.findViewById(R.id.image_message_profile); | |
} | |
void bind(UserMessage message) { | |
messageText.setText(message.getMessage()); | |
// Format the stored timestamp into a readable String using method. | |
timeText.setText(Utils.formatDateTime(message.getCreatedAt())); | |
nameText.setText(message.getSender().getNickname()); | |
// Insert the profile image from the URL into the ImageView. | |
Utils.displayRoundImageFromUrl(mContext, message.getSender().getProfileUrl(), profileImage); | |
} | |
} | |
} |
@mohammadeslim - use DateUtils()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what is Utils ?