Android聊天应用。我在客户端上使用一个临时ArrayList来确保显示聊天气泡按预期工作。我在哪里以及如何确定是否发送或接收邮件?
ChatConversationFragment:
3000
聊天对话消息:
messageArrayList.add(new ChatConversationMessage("Hello"));
messageArrayList.add(new ChatConversationMessage("Yeah"));
messageArrayList.add(new ChatConversationMessage("Yeah", R.drawable.redhead_1));
//this last one is received because it has two inputs
ConversationMessageAdapter:
/**
* The reason we have TWO of these methods with the same name is because sent
* messages don't have an image. We need to have a separate method for that.
**/
public ChatConversationMessage(String sentTextMessage) {
mSentTextMessage = sentTextMessage;
}
public ChatConversationMessage(String receivedTextMessage, int imageResourceId) {
mReceivedTextMessage = receivedTextMessage;
mImageResourceId = imageResourceId;
}
所以我试图将它放在最后一个类(适配器)的if语句中。但是我不知道该使用哪种方法以及将其放置在什么地方,因此我可以返回一个布尔值或某种使其根据发送或接收消息而显示不同的聊天气泡的东西。
答案 0 :(得分:0)
我为感兴趣的人找到了解决方案:
getView方法是唯一需要更改的方法。 我需要在if语句中使用“ setText”方法,以便为每个实例增加正确的布局。
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = null;
ChatConversationMessage currentMessage = getItem(position);
if (currentMessage.getSentTextMessage() != null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.chat_conversation_txt_sent, parent, false);
TextView sentText = listItemView.findViewById(R.id.textview_chat_message_sent);
sentText.setText(currentMessage.getSentTextMessage());
}
if (currentMessage.getReceivedTextMessage() !=null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.chat_conversation_txt_received, parent, false);
TextView receivedText = listItemView.findViewById(R.id.textview_chat_message_received);
receivedText.setText(currentMessage.getReceivedTextMessage());
}
return listItemView;
}
我不知道这是否是最好的方法,但是它有效,我认为它简洁明了且易于阅读。