我正在开发一个聊天应用程序。当您进入聊天时,您可以将所有收到的消息设置为已显示。但是在其他设备中如果我也在聊天中,则每条消息的布尔“看见”都不会更新。我的意思是当看到发送消息时,会出现“看到”的文本视图。但是我必须出去回到聊天活动才能看到textview。
这是onBindViewHolder void:
@Override
public void onBindViewHolder(@NonNull MessageViewHolder holder, int position) {
holder.isRecyclable();
Messages messages = userMessages.get(position);
String user_id = mAuth.getCurrentUser().getUid().toString();
String from_user_id = messages.getSender();
holder.seenView.setVisibility(View.GONE);
if(user_id.equals(from_user_id)){
//I send the message
holder.layout.setBackgroundResource(R.drawable.text_message_bg);
holder.messageText.setTextColor(Color.WHITE);
holder.msgParams.gravity = Gravity.RIGHT;
holder.messageText.setLayoutParams(holder.msgParams);
holder.dateParams.gravity = Gravity.RIGHT;
holder.messageDate.setLayoutParams(holder.dateParams);
holder.params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
if(messages.isSeen() && position == getItemCount() - 1){
holder.seenView.setVisibility(View.VISIBLE);
}
} else {
//I receive the message
holder.layout.setBackgroundResource(R.drawable.text_message_bg_2);
holder.messageText.setTextColor(Color.BLACK);
holder.msgParams.gravity = Gravity.LEFT;
holder.messageText.setLayoutParams(holder.msgParams);
holder.dateParams.gravity = Gravity.RIGHT;
holder.messageDate.setLayoutParams(holder.dateParams);
holder.params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
mDatabase.child(user_id).child(from_user_id).child("messages").child(messages.getId()).child("seen").setValue(true);
mDatabase.child(from_user_id).child(user_id).child("messages").child(messages.getId()).child("seen").setValue(true);
}
holder.getAdapterPosition();
//Poner texto
holder.messageText.setText(messages.getMessage());
holder.messageDate.setText(messages.getTime());
}
我的loadMessages无效:
private void loadMessages() {
mRootRef.child("messages").child(mCurrentUser).child(userKey).child("messages").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Messages messages = dataSnapshot.getValue(Messages.class);
messagesList.add(messages);
messageAdapter.notifyDataSetChanged();
recyclerView.scrollToPosition(messagesList.size() - 1);
}
@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) {
}
});
}
我试图在onChildChanged中添加“刷新”,但它只是添加了消息的项目副本......
我期待着您的回复!
编辑:我的整个适配器:
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {
private List<Messages> userMessages;
private FirebaseAuth mAuth;
private DatabaseReference mDatabase;
public MessageAdapter(List<Messages> userMessages) {
this.userMessages = userMessages;
}
public void setData(List<Messages> newData) {
this.userMessages.clear();
userMessages.addAll(newData);
notifyDataSetChanged();
}
@Override
public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_single_layout, parent, false);
mAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance().getReference().child("messages");
return new MessageViewHolder(V);
}
@Override
public void onBindViewHolder(@NonNull MessageViewHolder holder, int position) {
holder.isRecyclable();
Messages messages = userMessages.get(position);
String user_id = mAuth.getCurrentUser().getUid().toString();
String from_user_id = messages.getSender();
holder.seenView.setVisibility(View.GONE);
if(user_id.equals(from_user_id)){
//Yo envié el mensaje
holder.layout.setBackgroundResource(R.drawable.text_message_bg);
holder.messageText.setTextColor(Color.WHITE);
holder.msgParams.gravity = Gravity.RIGHT;
holder.messageText.setLayoutParams(holder.msgParams);
holder.dateParams.gravity = Gravity.RIGHT;
holder.messageDate.setLayoutParams(holder.dateParams);
holder.params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
if(messages.isSeen() && position == getItemCount() - 1){
holder.seenView.setVisibility(View.VISIBLE);
}
} else {
//Recivo el mensaje
holder.layout.setBackgroundResource(R.drawable.text_message_bg_2);
holder.messageText.setTextColor(Color.BLACK);
holder.msgParams.gravity = Gravity.LEFT;
holder.messageText.setLayoutParams(holder.msgParams);
holder.dateParams.gravity = Gravity.RIGHT;
holder.messageDate.setLayoutParams(holder.dateParams);
holder.params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
mDatabase.child(user_id).child(from_user_id).child("messages").child(messages.getId()).child("seen").setValue(true);
mDatabase.child(from_user_id).child(user_id).child("messages").child(messages.getId()).child("seen").setValue(true);
}
holder.getAdapterPosition();
//Poner texto
holder.messageText.setText(messages.getMessage());
holder.messageDate.setText(messages.getTime());
}
@Override
public int getItemCount() {
return userMessages.size();
}
public class MessageViewHolder extends RecyclerView.ViewHolder {
public TextView messageText;
public TextView messageDate;
public TextView seenView;
public LinearLayout layout;
public RelativeLayout.LayoutParams params;
public LinearLayout.LayoutParams dateParams;
public LinearLayout.LayoutParams msgParams;
public MessageViewHolder(View itemView) {
super(itemView);
messageText = itemView.findViewById(R.id.messageTextView);
messageDate = itemView.findViewById(R.id.dateTextView);
seenView = itemView.findViewById(R.id.visto);
layout = itemView.findViewById(R.id.messageSingleLayout);
params = (RelativeLayout.LayoutParams)layout.getLayoutParams();
dateParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
msgParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
}
}
答案 0 :(得分:0)
您只处理了添加新消息的事件,如果已更新,您必须处理该事件
module.js:549
throw err;
^
Error: Cannot find module
'C:\Users\akmal\AppData\Local\Temp\npm.5980\package\bin\npm-cli.js'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3