在 ChatActivity 中滚动后,我在聊天中发送的图像多次显示

After scrolling in ChatActivity the images which I have send in the chat are showing multiple times

我可以在对话中成功发送图片,但每当我滚动时,图片都会显示多次。我已成功从 firebase 中检索到图像的 URL。

这是我的 RecyclerViewViewHolder

public class MessageViewHolder extends RecyclerView.ViewHolder {

    public TextView messageText;
    public CircleImageView profileImage;
    public TextView displayName;
    public ImageView messageImage;

    public TextView timeText;

    public MessageViewHolder(View view) {
        super(view);

        timeText = view.findViewById(R.id.time_text_layout);
        messageText = (TextView) view.findViewById(R.id.message_text_layout);
        profileImage = (CircleImageView) view.findViewById(R.id.message_profile_layout);
        //   displayName = (TextView) view.findViewById(R.id.name_text_layout);
        messageImage = (ImageView) view.findViewById(R.id.message_image_layout);
    }
}

下面是 onBindViewHolder 和我的适配器的一些其他功能。

@Override
public void onBindViewHolder(final MessageViewHolder viewHolder, int i) {

    Messages c = mMessageList.get(i);

    final String from_user = c.getFrom();
    String message_type = c.getType();

    mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(from_user);
    mUserDatabase.keepSynced(true);
    mUserDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            // String name = dataSnapshot.child("name").getValue().toString();
            String image = dataSnapshot.child("thumb_image").getValue().toString();

            fuser = FirebaseAuth.getInstance().getCurrentUser();
            if (!from_user.equals(fuser.getUid())) {

                // viewHolder.displayName.setText(name);
                Picasso.with(viewHolder.profileImage.getContext()).load(image)
                        .placeholder(R.drawable.default_avatar).into(viewHolder.profileImage);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    if (message_type.equals("text")) {
        // viewHolder.messageText.setText(View.VISIBLE);
        viewHolder.messageText.setText(c.getMessage());
    } else {
        // viewHolder.messageText.setText(c.getMessage());
        viewHolder.messageImage.setVisibility(View.VISIBLE);
        Picasso.with(viewHolder.messageImage.getContext()).load(c.getMessage())
                .placeholder(R.drawable.default_avatar).into(viewHolder.messageImage);
    }
}

@Override
public int getItemCount() {
    return mMessageList.size();
}

@Override
public int getItemViewType(int position) {
    String fuser = FirebaseAuth.getInstance().getCurrentUser().getUid().toString();
    if (mMessageList.get(position).getFrom().equals(fuser)) {
        return MSG_TYPE_RIGHT;
    } else {
        return MSG_TYPE_LEFT;

    }
}

我没有收到此代码的任何错误。我已经尝试了所有方法,但无法阻止它多次显示图像。

在您的 onBindViewHolder 函数中,我认为您遗漏了 else 部分。您需要为 RecyclerView 的每个项目正确放置个人资料图片,如下所示。

if (!from_user.equals(fuser.getUid())) {
    Picasso.with(viewHolder.profileImage.getContext()).load(image).placeholder(R.drawable.default_avatar).into(viewHolder.profileImage);
} else {
    // Load the default avatar when the above condition fails.
    Picasso.with(viewHolder.profileImage.getContext()).load(R.drawable.default_avatar).into(viewHolder.profileImage);
}

另一个类似的错误出现在您代码的以下部分。

if(message_type.equals("text")) {
    // You need to set the visibility of the message image here as well
    viewHolder.messageImage.setVisibility(View.GONE);
    viewHolder.messageText.setText(c.getMessage());
} else {
    viewHolder.messageImage.setVisibility(View.VISIBLE);
    Picasso.with(viewHolder.messageImage.getContext()).load(c.getMessage()).placeholder(R.drawable.default_avatar).into(viewHolder.messageImage);
}

希望能解决您的问题。