content: package com.vain.android.ui;
  
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;
  import androidx.annotation.NonNull;
  import androidx.recyclerview.widget.RecyclerView;
  import java.util.ArrayList;
  import java.util.List;
  
  public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.MessageViewHolder> {
      private List<ChatMessage> messages;
  
      public ChatAdapter() {
          this.messages = new ArrayList<>();
      }
  
      @NonNull
      @Override
      public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          View view = LayoutInflater.from(parent.getContext())
                  .inflate(R.layout.item_message, parent, false);
          return new MessageViewHolder(view);
      }
  
      @Override
      public void onBindViewHolder(@NonNull MessageViewHolder holder, int position) {
          ChatMessage message = messages.get(position);
          holder.messageText.setText(message.getText());
          holder.timestampText.setText(message.getTimestamp());
      }
  
      @Override
      public int getItemCount() {
          return messages.size();
      }
  
      public void addMessage(ChatMessage message) {
          messages.add(message);
          notifyItemInserted(messages.size() - 1);
      }
  
      public void setMessages(List<ChatMessage> messages) {
          this.messages = messages;
          notifyDataSetChanged();
      }
  
      static class MessageViewHolder extends RecyclerView.ViewHolder {
          TextView messageText;
          TextView timestampText;
  
          MessageViewHolder(@NonNull View itemView) {
              super(itemView);
              messageText = itemView.findViewById(R.id.messageText);
              timestampText = itemView.findViewById(R.id.timestampText);
          }
      }
  }
  