107 lines
3.8 KiB
Java
107 lines
3.8 KiB
Java
|
|
package com.example.livestreaming;
|
||
|
|
|
||
|
|
import android.view.LayoutInflater;
|
||
|
|
import android.view.View;
|
||
|
|
import android.view.ViewGroup;
|
||
|
|
import android.widget.TextView;
|
||
|
|
|
||
|
|
import androidx.annotation.NonNull;
|
||
|
|
import androidx.recyclerview.widget.DiffUtil;
|
||
|
|
import androidx.recyclerview.widget.ListAdapter;
|
||
|
|
import androidx.recyclerview.widget.RecyclerView;
|
||
|
|
|
||
|
|
import java.text.SimpleDateFormat;
|
||
|
|
import java.util.Date;
|
||
|
|
import java.util.Locale;
|
||
|
|
|
||
|
|
public class ConversationMessagesAdapter extends ListAdapter<ChatMessage, RecyclerView.ViewHolder> {
|
||
|
|
|
||
|
|
private static final int TYPE_INCOMING = 1;
|
||
|
|
private static final int TYPE_OUTGOING = 2;
|
||
|
|
|
||
|
|
private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm", Locale.getDefault());
|
||
|
|
|
||
|
|
public ConversationMessagesAdapter() {
|
||
|
|
super(DIFF);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public int getItemViewType(int position) {
|
||
|
|
ChatMessage msg = getItem(position);
|
||
|
|
if (msg == null) return TYPE_INCOMING;
|
||
|
|
String u = msg.getUsername();
|
||
|
|
return "我".equals(u) ? TYPE_OUTGOING : TYPE_INCOMING;
|
||
|
|
}
|
||
|
|
|
||
|
|
@NonNull
|
||
|
|
@Override
|
||
|
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||
|
|
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
|
||
|
|
if (viewType == TYPE_OUTGOING) {
|
||
|
|
View v = inflater.inflate(R.layout.item_conversation_message_outgoing, parent, false);
|
||
|
|
return new OutgoingVH(v);
|
||
|
|
}
|
||
|
|
View v = inflater.inflate(R.layout.item_conversation_message_incoming, parent, false);
|
||
|
|
return new IncomingVH(v);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
|
||
|
|
ChatMessage msg = getItem(position);
|
||
|
|
if (holder instanceof IncomingVH) {
|
||
|
|
((IncomingVH) holder).bind(msg);
|
||
|
|
} else if (holder instanceof OutgoingVH) {
|
||
|
|
((OutgoingVH) holder).bind(msg);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static class IncomingVH extends RecyclerView.ViewHolder {
|
||
|
|
private final TextView nameText;
|
||
|
|
private final TextView msgText;
|
||
|
|
private final TextView timeText;
|
||
|
|
|
||
|
|
IncomingVH(@NonNull View itemView) {
|
||
|
|
super(itemView);
|
||
|
|
nameText = itemView.findViewById(R.id.nameText);
|
||
|
|
msgText = itemView.findViewById(R.id.messageText);
|
||
|
|
timeText = itemView.findViewById(R.id.timeText);
|
||
|
|
}
|
||
|
|
|
||
|
|
void bind(ChatMessage message) {
|
||
|
|
if (message == null) return;
|
||
|
|
nameText.setText(message.getUsername() != null ? message.getUsername() : "");
|
||
|
|
msgText.setText(message.getMessage() != null ? message.getMessage() : "");
|
||
|
|
timeText.setText(TIME_FORMAT.format(new Date(message.getTimestamp())));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static class OutgoingVH extends RecyclerView.ViewHolder {
|
||
|
|
private final TextView msgText;
|
||
|
|
private final TextView timeText;
|
||
|
|
|
||
|
|
OutgoingVH(@NonNull View itemView) {
|
||
|
|
super(itemView);
|
||
|
|
msgText = itemView.findViewById(R.id.messageText);
|
||
|
|
timeText = itemView.findViewById(R.id.timeText);
|
||
|
|
}
|
||
|
|
|
||
|
|
void bind(ChatMessage message) {
|
||
|
|
if (message == null) return;
|
||
|
|
msgText.setText(message.getMessage() != null ? message.getMessage() : "");
|
||
|
|
timeText.setText(TIME_FORMAT.format(new Date(message.getTimestamp())));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static final DiffUtil.ItemCallback<ChatMessage> DIFF = new DiffUtil.ItemCallback<ChatMessage>() {
|
||
|
|
@Override
|
||
|
|
public boolean areItemsTheSame(@NonNull ChatMessage oldItem, @NonNull ChatMessage newItem) {
|
||
|
|
return oldItem.getTimestamp() == newItem.getTimestamp();
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public boolean areContentsTheSame(@NonNull ChatMessage oldItem, @NonNull ChatMessage newItem) {
|
||
|
|
return oldItem.getTimestamp() == newItem.getTimestamp();
|
||
|
|
}
|
||
|
|
};
|
||
|
|
}
|