gpt4 book ai didi

java - Firestore 回收器适配器 - 滚动后项目消失

转载 作者:行者123 更新时间:2023-12-01 16:29:53 25 4
gpt4 key购买 nike

我正在尝试为我的项目创建聊天服务,为此,我将消息存储在从服务器获取的 Firestore 数据库中,并分配将发件人 ID 与当前用户 ID 进行比较的文本。当我开始 Activity 时,消息会正确显示,但是当我向下滚动时,我的消息会停止显示,或者其他用户的消息会消失,具体取决于是否打开聊天帐户。

我似乎无法弄清楚为什么会发生这种情况。

我的readMessages函数:

private void readMessages(){
Query query = DataRef.collection("rooms").document(roomID).collection("messages").orderBy("timestamp", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<MessageModel> options = new FirestoreRecyclerOptions.Builder<MessageModel>().setQuery(query, MessageModel.class).build();
adapter = new FirestoreRecyclerAdapter<MessageModel, MessageAdapter.ViewHolder>(options) {
@Override
protected void onBindViewHolder(MessageAdapter.ViewHolder holder, int position, MessageModel model) {
if (myUid.equals(model.getUid())) {
Log.d(TAG, "status1");
holder.msg_right.setText(String.valueOf(model.getMsg()));
holder.msg_left.setVisibility(View.INVISIBLE);
holder.profile_image.setVisibility(View.INVISIBLE);
holder.msg_nick.setVisibility(View.INVISIBLE);
}
else
{
DataRef.collection("users").document(String.valueOf(model.getUid())).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
DocumentSnapshot document = task.getResult();
msgNick = document.getString("usernm");
}
});
holder.msg_nick.setText(msgNick);
holder.msg_left.setText(String.valueOf(model.getMsg()));
holder.msg_right.setVisibility(View.INVISIBLE);
Log.d(TAG, "status0" );
}
}
@NonNull
@Override
public MessageAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message,parent,false);
return new MessageAdapter.ViewHolder(v);
}
};
adapter.startListening();
recyclerView.setAdapter(adapter);
}

我的消息适配器类:

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return null;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

}

@Override
public int getItemCount() {
return 0;
}

public static class ViewHolder extends RecyclerView.ViewHolder {

public TextView msg_right;
public ImageView profile_image;
public TextView msg_left;
public TextView msg_nick;

public ViewHolder(View itemView) {
super(itemView);

msg_right = itemView.findViewById(R.id.msg_right);
profile_image = itemView.findViewById(R.id.msg_leftavatar);
msg_left = itemView.findViewById(R.id.msg_left);
msg_nick = itemView.findViewById(R.id.msg_nick);
}
}
}

向下和向上滚动之前的聊天状态:

"State of the chat before scrolling down and up

这样做之后:

And after doing so

最佳答案

请尝试将设置holder成员的代码块移到else语句中的回调监听器中。

private void readMessages(){
Query query = DataRef.collection("rooms").document(roomID).collection("messages").orderBy("timestamp", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<MessageModel> options = new FirestoreRecyclerOptions.Builder<MessageModel>().setQuery(query, MessageModel.class).build();
adapter = new FirestoreRecyclerAdapter<MessageModel, MessageAdapter.ViewHolder>(options) {
@Override
protected void onBindViewHolder(MessageAdapter.ViewHolder holder, int position, MessageModel model) {
if (myUid.equals(model.getUid())) {
Log.d(TAG, "status1");
holder.msg_right.setText(String.valueOf(model.getMsg()));
holder.msg_left.setVisibility(View.INVISIBLE);
holder.profile_image.setVisibility(View.INVISIBLE);
holder.msg_nick.setVisibility(View.INVISIBLE);
}
else
{
DataRef.collection("users").document(String.valueOf(model.getUid())).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
DocumentSnapshot document = task.getResult();
msgNick = document.getString("usernm");
holder.msg_nick.setText(msgNick);
holder.msg_left.setText(String.valueOf(model.getMsg()));
holder.msg_right.setVisibility(View.INVISIBLE);
Log.d(TAG, "status0" ); }
});

}
}

关于java - Firestore 回收器适配器 - 滚动后项目消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62070089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com