我正在尝试为聊天应用实现FirebaseRecyclerAdapter
,我想在用户发送新消息时应用一种算法,该消息应该与最近发送的消息一起出现在列表的顶部。我能够实现它的问题是
它显示了最近的消息三遍,但是随后以相同的最新消息开始setText
到其他项目。如我所使用的orderByChild
()方法。这是代码
///在这里设置适配器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
DatabaseReference userChat = UserFirebase.databaseReference().child("Test").child(shdUser.getString("user_id", ""));
FirebaseRecyclerOptions options = new FirebaseRecyclerOptions.Builder<PojoNewChatMessage>().setQuery(userChat.orderByChild("timestamp"), PojoNewChatMessage.class).build();
adapterFirebaseAllChat = new AdapterFirebaseAllChat(options, getFragmentManager(), shdUser.getString("user_id", ""), getContext());
recyclerView.setAdapter(adapterFirebaseAllChat);
adapterFirebaseAllChat.startListening();
这是适配器
public class AdapterFirebaseAllChat extends FirebaseRecyclerAdapter<PojoNewChatMessage, AdapterFirebaseAllChat.Myholder> {
DatabaseReference userDetails, userMessage, recentMessageRef;
Context context;
private FragmentManager supportFragmentManager;
SharedPreferences shdUser;
SelectSingleItemCallback selectSingleItemCallback;
public void setSelectSingleItemCallback(SelectSingleItemCallback selectSingleItemCallback) {
this.selectSingleItemCallback = selectSingleItemCallback;
}
public AdapterFirebaseAllChat(@NonNull FirebaseRecyclerOptions<PojoNewChatMessage>
options, FragmentManager supportFragmentManager, String userId, Context context) {
super(options);
this.context = context;
shdUser = context.getSharedPreferences("user", Context.MODE_PRIVATE);
userDetails = UserFirebase.databaseReference().child("User");
userMessage = UserFirebase.databaseReference().child("Message").child(userId);
recentMessageRef = UserFirebase.databaseReference().child("RecentMessage").child(shdUser.getString("user_id", ""));
this.supportFragmentManager = supportFragmentManager;
}
@Override
protected void onBindViewHolder(@NonNull final Myholder holder, int position, @NonNull PojoNewChatMessage model) {
final String userId = getRef(position).getKey();
selectSingleItemCallback.selectedItem(String.valueOf(getItemCount()));
recentMessageRef.child(userId).child("recentMessage").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
String message = dataSnapshot.child("message").getValue().toString();
String type = dataSnapshot.child("type").getValue().toString();
String time = dataSnapshot.child("time").getValue().toString();
holder.txt_time.setText(time);
holder.txt_msg_remainingcount.setVisibility(View.GONE);
if (type.equals("txt")) {
holder.txt_last_msg.setText(message);
} else if (type.equals("pdf")) {
holder.txt_last_msg.setText("PDF");
} else if (type.equals("video")) {
holder.txt_last_msg.setText("Video");
} else if (type.equals("contact")) {
holder.txt_last_msg.setText("Contact");
} else if (type.equals("audio")) {
holder.txt_last_msg.setText("Audio");
} else {
holder.txt_last_msg.setText("");
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
userDetails.child(userId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
final String name = dataSnapshot.child("name").getValue().toString();
final String img = dataSnapshot.child("image").getValue().toString();
holder.txt_name.setText(name);
Picasso.get().load(img).into(holder.img_dp);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
context.startActivity(new Intent(context, SingleChatActivity.class)
.putExtra("other_id", userId)
.putExtra("name", name)
.putExtra("icon", img)
);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
DialogeActionOnParticipants actionOnChat = new DialogeActionOnParticipants();
final List<String> data1 = new ArrayList<>();
data1.add("View Profile");
data1.add("Delete chat");
actionOnChat.setData(data1);
actionOnChat.show(supportFragmentManager, "dsddfs");
actionOnChat.setSelectSingleItemCallback(new SelectSingleItemCallback() {
@Override
public void selectedItem(String item) {
if (item.equals("View Profile")) {
context.startActivity(new Intent(context, VisitorUserProfile.class)
.putExtra("visitor_id", userId)
);
}
if (item.equals("Delete chat")) {
userMessage.child(userId).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
selectSingleItemCallback.selectedItem(String.valueOf(getItemCount()));
}
});
}
}
});
return false;
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@NonNull
@Override
public Myholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
context = viewGroup.getContext();
return new Myholder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_chat_single_user_chat_view, viewGroup, false));
}
public class Myholder extends RecyclerView.ViewHolder {
CircleImageView img_dp;
TextView txt_name, txt_time, txt_last_msg, txt_msg_remainingcount;
public Myholder(@NonNull View itemView) {
super(itemView);
img_dp = itemView.findViewById(R.id.img_dp);
txt_name = itemView.findViewById(R.id.txt_name);
txt_time = itemView.findViewById(R.id.txt_time);
txt_last_msg = itemView.findViewById(R.id.txt_last_msg);
txt_msg_remainingcount = itemView.findViewById(R.id.txt_msg_remainingcount);
}
}
@Override
public void onViewRecycled(@NonNull Myholder holder) {
}
}