Answer the question
In order to leave comments, you need to log in
How to implement sending and receiving messages in the chat?
Hello! I understand the logic of sending and receiving messages in order to receive and send messages to the chat. I just can’t understand why the messages that I receive arrive from the right (from the same side that I send). Who will tell you how to register to recognize received messages and place them on the left? I am posting my code below.
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ChatViewHolder> {
public static final int MSG_TYPE_LEFT = 1;
public static final int MSG_TYPE_RIGHT = 0;
ArrayList<String> messages;
LayoutInflater inflater;
DataChat dataChat;
public DataAdapter(Context context, ArrayList<String> messages) {
this.messages = messages;
this.inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public ChatViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
switch (viewType) {
case MSG_TYPE_RIGHT:
View view = inflater.inflate(R.layout.item_chat, parent, false);
return new ChatViewHolder(view);
case MSG_TYPE_LEFT:
View v = inflater.inflate(R.layout.item_chat_left, parent, false);
return new ChatViewHolder(v);
default:
return null;
}
}
@Override
public void onBindViewHolder(@NonNull ChatViewHolder holder, int position) {
String msg = messages.get(position);
holder.tvMessage.setText(msg);
}
@Override
public int getItemCount() {
return messages.size();
}
@Override
public final int getItemViewType(int position) {
if (dataChat.isMine()) return MSG_TYPE_RIGHT;
else return MSG_TYPE_LEFT;
}
class ChatViewHolder extends RecyclerView.ViewHolder {
TextView tvMessage;
ChatViewHolder(View itemView) {
super(itemView);
tvMessage = itemView.findViewById(R.id.tvMessage);
}
}
static class DataChat {
private boolean isMine;
private String content;
DataChat(String message, boolean mine) {
content = message;
isMine = mine;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public boolean isMine() {
return isMine;
}
public void setIsMine(boolean isMine) {
this.isMine = isMine;
}
}
}
public class MessageActivity extends AppCompatActivity implements View.OnClickListener {
RelativeLayout relative_layout;
Button btn_message_chat;
EditText et_message, edId;
ImageView iv_smile_chat, iv_camera_chat, iv_gallery_chat, iv_gif_chat, iv_highsignal_chat;
ListView listView;
TextView tvMessage;
RecyclerView recyclerView;
WebSocket ws;
private static final String TAG = "My Log Test";
private ArrayList<String> arrayList = new ArrayList<>();
private DataAdapter dataAdapter;
private MessageActivityViewModel messageActivityViewModel;
public SocketManger sm;
private static Handler h;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_send);
messageActivityViewModel = ViewModelProviders.of(this).get(MessageActivityViewModel.class);
// Передача данных в LiveData
MutableLiveData<List<Message>> liveDataLoadHistory = messageActivityViewModel.getLiveDataLoadHistory();
liveDataLoadHistory.observe(this, new Observer<List<Message>>() {
@Override
public void onChanged(List<Message> messages) {
for (Message msg: messages) {
arrayList.add(msg.getText());
}
dataAdapter.notifyDataSetChanged();
}
});
messageActivityViewModel.getLiveDataSendMsg().observe(this, new Observer<String>(){
@Override
public void onChanged(String s) {
if(s.equals(ENTER_EMPTY_MSG)){
Toast.makeText(getApplicationContext(),
"Вы ввели пустое сообщение", Toast.LENGTH_LONG)
.show();
return;
}
if(s.equals(VERY_LONG_MSG)) {
Toast.makeText(getApplicationContext(),
"Вы ввели очень длинное", Toast.LENGTH_LONG)
.show();
return;
}
arrayList.add(s);
dataAdapter.notifyDataSetChanged();
int newMsgPosition = arrayList.size() - 1;
dataAdapter.notifyItemInserted(newMsgPosition);
recyclerView.scrollToPosition(newMsgPosition);
et_message.setText("");
}
});
App myApp = (App) getApplication();
sm = myApp.getSocketManger();
sm.setHandler(h);
listView = (ListView) findViewById(R.id.listView);
recyclerView = (RecyclerView) findViewById(R.id.message_recycler);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplication());
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
dataAdapter = new DataAdapter(getApplicationContext(), arrayList );
recyclerView.setAdapter(dataAdapter);
et_message = (EditText) findViewById(R.id.et_message);
relative_layout = (RelativeLayout) findViewById(R.id.relative_layout);
tvMessage = (TextView) findViewById(R.id.tvMessage);
iv_smile_chat = (ImageView) findViewById(R.id.iv_smile_chat);
iv_camera_chat = (ImageView) findViewById(R.id.iv_camera_chat);
iv_gallery_chat = (ImageView) findViewById(R.id.iv_gallery_chat);
iv_gif_chat = (ImageView) findViewById(R.id.iv_gif_chat);
iv_highsignal_chat = (ImageView) findViewById(R.id.iv_highsignal_chat);
btn_message_chat = (Button) findViewById(R.id.btn_message_chat);
edId = (EditText)findViewById(R.id.edId);
iv_smile_chat.setOnClickListener(this);
btn_message_chat.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String method = "message/send";
String to = edId.getText().toString();
String text = et_message.getText().toString();
String attachments = "";
String type = "";
String file_id = "";
String message_id = "";
String url = "";
String vote_id = "";
String product_id = "";
String user_id = "";
String group_id = "";
messageActivityViewModel.sendMessageToServer(method, to, text, attachments, type, file_id, message_id, url,
vote_id, product_id, user_id, group_id);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question