我希望允许用户彼此发送消息。到目前为止,我已经设置了接收方和发送方的布局以及回收视图。当用户尝试向某人发送消息时,该消息将被发送并成功加载到回收视图中,但是接收者没有收到该消息,但是对于发送者来说确实会出现。我基本上尝试这样做的方式是,当用户选择要发送消息的人时,我将所选用户的UID存储为接收者,而发送者将是当前登录的用户。
//Loads data into views
@Override
public void onBindViewHolder(@NonNull MessageViewHolder viewHolder, int i) {
//logged in user will be the sender of the message
String senderOfMessage = mAuth.getCurrentUser().getUid();
Message message= userMessagesList.get(i);
String receiverOfMessage = message.getReceiver();
if(receiverOfMessage.equals(senderOfMessage)){
viewHolder.senderTextView.setVisibility(View.VISIBLE);
viewHolder.senderTextView.setBackgroundResource(R.drawable.sender_messages_layout);
viewHolder.senderTextView.setTextColor(Color.BLACK);
viewHolder.senderTextView.setText(message.getMessage());
}else{
viewHolder.receiverTextView.setVisibility(View.VISIBLE);
viewHolder.receiverTextView.setBackgroundResource(R.drawable.receiver_messages_layout);
viewHolder.receiverTextView.setTextColor(Color.BLACK);
viewHolder.receiverTextView.setText(message.getMessage());
}
}
这就是我保存邮件的方式
//saves messages to firebase
public void saveMessages() {
//store data under timestamp to prevent overriding current information from Firebase...
SimpleDateFormat formatter = new SimpleDateFormat(" HH:mm:ss ");
Date date = new Date(System.currentTimeMillis());
messageTimeStamp = currentFirebaseUser.getUid() + formatter.format(date);
String messageSender = currentFirebaseUser.getUid();
String messeaageReceiver = id;
messageDictionary.put("message", messageInputEditText.getText().toString());
messageDictionary.put("sender", currentFirebaseUser.getUid());
messageDictionary.put("receiver", id);
messesagesRef.child("Messages").child(messageSender).child(messeaageReceiver).push().setValue(messageDictionary);
messageInputEditText.setText("");
}
这就是我的检索方式
final String messageSender = currentFirebaseUser.getUid();
final String messeaageReceiver = id;
//cleared message b4 to prevent duplication...
messageList.clear();
firebaseDatabase.child("Messages").child(messageSender).child(messeaageReceiver).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
messages = dataSnapshot.getValue(Message.class);
messageList.add(messages);
messageAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
**数据库结构**
qnrE4PTKbNdKNs33cKtx1qLEvnS2
C0EBxNAEmahhTEztCIu1yNg4wQz1
-LpBiC7SRcHuoxzPAxN8
message: "Hi"
receiver: "C0EBxNAEmahhTEztCIu1yNg4wQz1"
sender: "qnrE4PTKbNdKNs33cKtx1qLEvnS2"
答案 0 :(得分:0)
也许您可以尝试实现Recycler视图的方法getItemViewType
,并在那里使用您的概念定义布局类型。
getItemViewType
返回视图类型的整数,因此您可以定义最终整数
public static final int RECEIVER = 1;
public static final int SENDER = 2;
现在使用概念检查项目视图类型
@Override
public int getItemViewType(int position) {
String senderOfMessage = mAuth.getCurrentUser().getUid();
Message message= userMessagesList.get(position);
String receiverOfMessage = message.getReceiver();
if(receiverOfMessage.equals(senderOfMessage))
return [SENDER OR RECEIVER according to your condition]
else
return [SENDER OR RECEIVER according to your condition]
return -1;
}
现在使用getItemViewType(i)
作为onBindViewHolder
中的第一条语句来显示各自的布局。
@Override
public void onBindViewHolder(@NonNull MessageViewHolder viewHolder, int i) {
int itemViewType = getItemViewType(i);
if(itemViewType == [RECEIVER or SENDER]){
viewHolder.senderTextView.setVisibility(View.VISIBLE);
viewHolder.senderTextView.setBackgroundResource(R.drawable.sender_messages_layout);
viewHolder.senderTextView.setTextColor(Color.BLACK);
viewHolder.senderTextView.setText(message.getMessage());
} else{
viewHolder.receiverTextView.setVisibility(View.VISIBLE);
viewHolder.receiverTextView.setBackgroundResource(R.drawable.receiver_messages_layout);
viewHolder.receiverTextView.setTextColor(Color.BLACK);
viewHolder.receiverTextView.setText(message.getMessage());
}
}