为什么我只在片段中显示一次我的用户列表,当我更改片段并返回时,它不会显示用户列表。
该功能正在工作,但我不知道为什么只显示一次。
private RecyclerView recyclerView;
FirebaseUser firebaseUser;
private MessageListAdapter messageListAdapter;
private List<ChatListModel> usersList;
private List<UserModel> mUsers;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_message, container,false);
recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
usersList = new ArrayList<>();
DatabaseReference reference =
FirebaseDatabase.getInstance().getReference("Chatlist").child(firebaseUser.getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
usersList.clear();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
ChatListModel chatListModel = snapshot.getValue(ChatListModel.class);
usersList.add(chatListModel);
}
chatList();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
return view;
}
private void chatList() {
mUsers = new ArrayList<>();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mUsers.clear();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
UserModel userModel = snapshot.getValue(UserModel.class);
for(ChatListModel chatListModel : usersList){
if(userModel.getId().equals(chatListModel.getId())){
mUsers.add(userModel);
}
}
}
messageListAdapter = new MessageListAdapter(getContext(), mUsers);
recyclerView.setAdapter(messageListAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
我从来没有撞车过,只是换了另一个碎片,把它放回去。片段将显示空白页。
暂无答案!
目前还没有任何答案,快来回答吧!