单击recyclerview上的项目时的多个数据
Posted
技术标签:
【中文标题】单击recyclerview上的项目时的多个数据【英文标题】:Multiple data when clicked item on recyclerview 【发布时间】:2020-10-23 20:29:19 【问题描述】:我尝试显示聊天列表,但是当我单击项目时,recyclerview 中的列表将重复,如图 1 所示,但是当我更改片段(底部导航)时,列表是正常的(不重复)。我的代码有什么问题,请帮忙
after item clicked before item clicked
这里是代码 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)
);
【问题讨论】:
【参考方案1】:使用“adapterChatlist.setLastMsgMap(userId, theLastMsg);”您正在将一些新用户添加到现有列表中。我的猜测是,您应该拥有一个包含所有要显示的项目的列表,并始终清除适配器拥有的旧列表。因为这个 onDataChange() 可能会被多次调用,而不仅仅是一次,这就是你获得更多项目的原因。
【讨论】:
以上是关于单击recyclerview上的项目时的多个数据的主要内容,如果未能解决你的问题,请参考以下文章
如何在单击 RecyclerView 项目时从一个片段移动到另一个片段
RecyclerView android上的MultichoiceModeListener
获取 ArrayList 值并将其设置为按钮单击时的 TextView 不工作 - RecyclerView
在使用 FirebaseRecyclerPagingAdapter 时,第二次单击 RecyclerView 中的项目时,片段显示为空
在 recyclerview 项目上单击 show recyclerview data custom in toDialog Box