我正在做一个聊天应用。同一观看者有2个视图。
问题是,当我滚动到顶部时,图像和名称未正确插入。
我将Firebase Ui Recycler View用于实时数据库。

firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AdminMessage, AdminChatViewHolder>(firebaseRecyclerOptions) {
                @Override
                protected void onBindViewHolder(@NonNull final AdminChatViewHolder adminChatViewHolder, int i, @NonNull AdminMessage adminMessage) {

adminChatViewHolder.getTvMessage().setText(adminMessage.getMessage());



                //We retrieve the userUid to get real time name
                FirebaseDatabase.getInstance().getReference().child("admin").child(adminMessage.getSenderUid()).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if (dataSnapshot.exists()) {
                            Admin admin = dataSnapshot.getValue(Admin.class);
                            if (admin != null) {
                                final String nameAdmin = admin.getFullName();
                                final String profileUrl = admin.getProfileUrl();

                                //And then display
                                adminChatViewHolder.getTvName().setText(nameAdmin);

                                //Check for image if null or not (profileUrl)
                                if (profileUrl != null) {
                                    Glide.with(getApplicationContext())
                                            .load(profileUrl)
                                            .into(adminChatViewHolder.getImageView());
                                }
                            }
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

            }


            @NonNull
            @Override
            public AdminChatViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view;
                if (viewType == VT_SENDER) {
                    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_admin_message_sender, parent, false);
                } else {
                    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_admin_message_receiver, parent, false);
                }
                return new AdminChatViewHolder(view);
            }

            @Override
            public void onDataChanged() {
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public int getItemViewType(int position) {
                if (getItem(position).getSenderUid().equals(userUid)) {
                    return VT_SENDER;
                }
                return VT_RECEIVER;
            }
        };

最佳答案

好吧,我记得我之前也遇到过同样的问题。我将尽我所能记住并更新此答案。

当没有图片或网址失败时,请始终清空图片视图或将其隐藏:

if (profileUrl != null) {
   Glide.with(getApplicationContext())
        .load(profileUrl)
        .into(adminChatViewHolder.getImageView());
}
else
{
  //TODO: set an image place holder or hide adminChatViewHolder.getImageView()
}


我喜欢在等待新图像加载时清空imageView
并对所有视图(不仅是imageView(即textview))执行相同操作
如果admin为null怎么办?怎么了?

不要忘记拨打super.onDataChanged();

09-05 02:55