Created
June 4, 2019 19:33
-
-
Save AlexPrestonSB/54a37722c9d475f52f3cd88522b4e045 to your computer and use it in GitHub Desktop.
This file contains 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
private class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { | |
private static final int VIEW_TYPE_MESSAGE_SENT = 1; | |
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2; | |
private ArrayList<BaseMessage> mMessageList; | |
private OpenChannel mChannel; | |
ChatAdapter(OpenChannel channel) { | |
mMessageList = new ArrayList<>(); | |
mChannel = channel; | |
refresh(); | |
} | |
.... | |
// 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); | |
} | |
} | |
@Override | |
public int getItemCount() { | |
return mMessageList.size(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment