使用 Firebase 在我的聊天应用程序中杂乱无章的聊天消息
Disorganized Chat messages in my Chat App using Firebase
我最近添加了一个发送图片的功能以及发送短信的选项。在此之前一切正常 - 消息按预期按时间递减弹出。但是在我添加了这个图片发送功能之后,每当我向上或向下滚动聊天时,消息都会变得混乱,我不知道为什么。
当我只发送文本时:
滚动前https://i.stack.imgur.com/2NMVHl.jpg
滚动后https://i.stack.imgur.com/2Nxpc.jpg(相同)
当我发送带文字的图片时
滚动前https://i.stack.imgur.com/rS6Mx.jpg
滚动后https://i.stack.imgur.com/OP5DK.jpg
在我的代码中有两个地方可以上传消息(图片或文本)。
- 发送图片消息:
Briefings - Here I upload the image to the Storage and then retrieve its URL. Then I store the URL to firestore in form of string (which I later use to download the image using Picasso) along with other message details.
final Long currentTime = System.currentTimeMillis();
final String time = currentTime + "";
final StorageReference fileref = storageReference.child("Image Messages")
.child(uid + time);
StorageTask uploadTask = fileref.putFile(uri);
uploadTask.continueWithTask(new Continuation() {
@Override
public Object then(@NonNull Task task) throws Exception {
if(!task.isSuccessful()){
throw task.getException();
}
return fileref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if(task.isSuccessful()){
Uri downloadUrl = task.getResult();
String myUrl = downloadUrl.toString();
Map<String, Object> chat = new HashMap<>();
chat.put("message", myUrl);
chat.put("time", currentTime);
chat.put("sender", mUser.getUid());
chat.put("groupId",Gid);
chat.put("type","image");
chat.put("username",preferences.getData("username"));
chat.put("name",preferences.getData("usernameAdded"));
firestore.collection("aGroups").document(Gid).collection("Chat")
.add(chat).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
showChatMessages();
dialog.dismiss();
Toast.makeText(getApplicationContext(),"Message Sent", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_SHORT).show();
}
});
}
}
});
- 用于发送短信
Briefings - Here I upload the message and it's details
Map<String, Object> chat = new HashMap<>();
chat.put("message", message.getText().toString());
chat.put("time", System.currentTimeMillis());
chat.put("sender", mUser.getUid());
chat.put("groupId",Gid);
chat.put("type","text");
chat.put("username",preferences.getData("username"));
chat.put("name",preferences.getData("usernameAdded"));
firestore.collection("aGroups").document(Gid).collection("Chat")
.add(chat).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(ChatInterface.this, "Message Sent", Toast.LENGTH_SHORT).show();
showChatMessages();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_SHORT).show();
}
});
这是我添加回调的地方
private void showChatMessages() {
firestore.collection("aGroups").document(Gid).collection("Chat")
.orderBy("time", Query.Direction.DESCENDING)
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
if(error != null){
Log.d("Check", "listen failed: " + error.getMessage());
}
else{
Log.d("Check", "Snapshot worked");
List<ChatModel> list = new ArrayList<>();
list.clear();
for(QueryDocumentSnapshot query : value){
list.add(new ChatModel(
query.getString("groupId")
, query.getId()
, query.getString("message")
, query.getString("sender")
, query.getLong("time")
, query.getString("name")
, query.getString("username")
, query.getString("type")
));
}
recycler_interface.setAdapter(new RealChatRecyclerInterface(mUser.getUid(),list));
}}
});
}
我将整个 RealChatRecyclerInterface
添加到 this pastebin link。
在您的 RealChatRecyclerInterface
中使用 holder.setIsRecyclable(false);
我最近添加了一个发送图片的功能以及发送短信的选项。在此之前一切正常 - 消息按预期按时间递减弹出。但是在我添加了这个图片发送功能之后,每当我向上或向下滚动聊天时,消息都会变得混乱,我不知道为什么。
当我只发送文本时:
滚动前https://i.stack.imgur.com/2NMVHl.jpg
滚动后https://i.stack.imgur.com/2Nxpc.jpg(相同)
当我发送带文字的图片时
滚动前https://i.stack.imgur.com/rS6Mx.jpg
滚动后https://i.stack.imgur.com/OP5DK.jpg
在我的代码中有两个地方可以上传消息(图片或文本)。
- 发送图片消息:
Briefings - Here I upload the image to the Storage and then retrieve its URL. Then I store the URL to firestore in form of string (which I later use to download the image using Picasso) along with other message details.
final Long currentTime = System.currentTimeMillis();
final String time = currentTime + "";
final StorageReference fileref = storageReference.child("Image Messages")
.child(uid + time);
StorageTask uploadTask = fileref.putFile(uri);
uploadTask.continueWithTask(new Continuation() {
@Override
public Object then(@NonNull Task task) throws Exception {
if(!task.isSuccessful()){
throw task.getException();
}
return fileref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if(task.isSuccessful()){
Uri downloadUrl = task.getResult();
String myUrl = downloadUrl.toString();
Map<String, Object> chat = new HashMap<>();
chat.put("message", myUrl);
chat.put("time", currentTime);
chat.put("sender", mUser.getUid());
chat.put("groupId",Gid);
chat.put("type","image");
chat.put("username",preferences.getData("username"));
chat.put("name",preferences.getData("usernameAdded"));
firestore.collection("aGroups").document(Gid).collection("Chat")
.add(chat).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
showChatMessages();
dialog.dismiss();
Toast.makeText(getApplicationContext(),"Message Sent", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_SHORT).show();
}
});
}
}
});
- 用于发送短信
Briefings - Here I upload the message and it's details
Map<String, Object> chat = new HashMap<>();
chat.put("message", message.getText().toString());
chat.put("time", System.currentTimeMillis());
chat.put("sender", mUser.getUid());
chat.put("groupId",Gid);
chat.put("type","text");
chat.put("username",preferences.getData("username"));
chat.put("name",preferences.getData("usernameAdded"));
firestore.collection("aGroups").document(Gid).collection("Chat")
.add(chat).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(ChatInterface.this, "Message Sent", Toast.LENGTH_SHORT).show();
showChatMessages();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_SHORT).show();
}
});
这是我添加回调的地方
private void showChatMessages() {
firestore.collection("aGroups").document(Gid).collection("Chat")
.orderBy("time", Query.Direction.DESCENDING)
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
if(error != null){
Log.d("Check", "listen failed: " + error.getMessage());
}
else{
Log.d("Check", "Snapshot worked");
List<ChatModel> list = new ArrayList<>();
list.clear();
for(QueryDocumentSnapshot query : value){
list.add(new ChatModel(
query.getString("groupId")
, query.getId()
, query.getString("message")
, query.getString("sender")
, query.getLong("time")
, query.getString("name")
, query.getString("username")
, query.getString("type")
));
}
recycler_interface.setAdapter(new RealChatRecyclerInterface(mUser.getUid(),list));
}}
});
}
我将整个 RealChatRecyclerInterface
添加到 this pastebin link。
在您的 RealChatRecyclerInterface
中使用holder.setIsRecyclable(false);