[英]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.