[英]Multiple data when clicked item on recyclerview
我嘗試顯示聊天列表,但是當我單擊項目時,recyclerview 中的列表是重復的,如圖 1 所示,但是當我更改片段(底部導航)時,列表是正常的(不重復)。 我的代碼有什么問題,請幫忙
這里的代碼AdapterChatlist
public class AdapterChatlist extends RecyclerView.Adapter<AdapterChatlist.Holder>{
Context context;
List<User> userList;//get user info
private HashMap<String, String> lastMsgMap;
public AdapterChatlist(Context context, List<User> userList) {
this.context = context;
this.userList = userList;
lastMsgMap = new HashMap<>();
}
@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//inflate layout row_chatlist
View view = LayoutInflater.from(context).inflate(R.layout.row_chatlist, parent, false);
return new Holder(view);
}
@Override
public void onBindViewHolder(@NonNull Holder holder, int position) {
String hisUid = userList.get(position).getId();
String userImg = userList.get(position).getImageUrl();
String userName = userList.get(position).getFullname();
String lastMsg = lastMsgMap.get(hisUid);
holder.nameTv.setText(userName);
if (lastMsg==null || lastMsg.equals("default")){
holder.lastMsgTv.setVisibility(View.GONE);
}else {
holder.lastMsgTv.setVisibility(View.VISIBLE);
holder.lastMsgTv.setText(lastMsg);
}
try {
Glide.with(context).load(userImg)
.placeholder(R.drawable.ic_pic)
.into(holder.profileIv);
}catch (Exception e){
Glide.with(context).load(R.drawable.ic_pic)
.into(holder.profileIv);
}
if (userList.get(position).getOnlineStat().equals("online")){
holder.onlineStatIv.setImageResource(R.drawable.ic_online);
}else {
holder.onlineStatIv.setImageResource(R.drawable.ic_offline);
}
holder.itemView.setOnClickListener(view -> {
Intent intent = new Intent(context, MessageActivity.class);
intent.putExtra("userid", hisUid);
context.startActivity(intent);
});
}
public void setLastMsgMap(String userId, String lastMsg){
lastMsgMap.put(userId, lastMsg);
}
@Override
public int getItemCount() {
return userList.size();
}
class Holder extends RecyclerView.ViewHolder{
ImageView profileIv, onlineStatIv;
TextView nameTv, lastMsgTv;
public Holder(@NonNull View itemView) {
super(itemView);
profileIv = itemView.findViewById(R.id.profileIv);
onlineStatIv = itemView.findViewById(R.id.onlineStatIv);
nameTv = itemView.findViewById(R.id.nameTv);
lastMsgTv = itemView.findViewById(R.id.lastMsgTv);
}
}}
這里是ChatlistFragement的代碼
private void loadChats() {
usersList = new ArrayList<>();
ref = FirebaseDatabase.getInstance().getReference("Users");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot ds : snapshot.getChildren()) {
User users = ds.getValue(User.class);
for (Chatlist chatlist : chatlistList) {
if (users.getId() != null && users.getId().equals(chatlist.getId())) {
usersList.add(users);
break;
}
}
adapterChatlist = new AdapterChatlist(getContext(), usersList);
//setAdapter
rv_chatsList.setAdapter(adapterChatlist);
//set last msg
for (int i = 0; i < usersList.size(); i++) {
lastMsg(usersList.get(i).getId());
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void lastMsg(final String userId) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Chats");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String theLastMsg = "default";
for (DataSnapshot ds : snapshot.getChildren()) {
Chat chat = ds.getValue(Chat.class);
if (chat == null) {
continue;
}
String sender = chat.getSender();
String receiver = chat.getReceiver();
if (sender == null || receiver == null) {
continue;
}
if (chat.getReceiver().equals(currentUser.getUid()) && chat.getSender().equals(userId) ||
chat.getReceiver().equals(userId) && chat.getSender().equals(currentUser.getUid())) {
theLastMsg = chat.getMessage();
}
}
adapterChatlist.setLastMsgMap(userId, theLastMsg);
adapterChatlist.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
使用“adapterChatlist.setLastMsgMap(userId, theLastMsg);” 您正在將一些新用戶添加到現有列表中。 我的猜測是,您應該有一個包含所有要顯示的項目的列表,並始終清除適配器擁有的舊列表。 因為這個 onDataChange() 可能不止一次被調用多次,這就是你得到更多項目的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.