滚动时FirebaseRecyclerAdapter,图像开始出现在所有viewHolder中未加协议

时间:2017-05-01 16:20:11

标签: android firebase firebase-realtime-database firebaseui

在聊天应用程序中,我有一个消息项,每条消息可以包含一个文本字段,或文本和图像...我列出了使用FirebaseRecyclerAdapter的项目,并且每件事都可以,仅包含文本的消息仅显示文本,以及带图像的消息显示文本和图像...但是,当我向上或向下滚动时,图像开始出现在其他消息中不受控制,不包含图像的消息,所有消息显示图像,如何可能,任何人都知道为什么这发生了吗?

继承我的firebaseadapter:

mFirebaseAdapter = new FirebaseRecyclerAdapter<ForumMessage,MessageViewHolder>(
            ForumMessage.class,
            R.layout.podforum_item,
            MessageViewHolder.class,
            storeOffers) {

        @Override
        protected void populateViewHolder(final MessageViewHolder viewHolder,
                                          ForumMessage friendlyMessage, int position) {


            viewHolder.messageTextView.setText(friendlyMessage.getBody());
            viewHolder.messageTextView.setVisibility(TextView.VISIBLE);
            //viewHolder.messageImageView.setVisibility(ImageView.GONE);

            if (friendlyMessage.getDodataSlika() != null){
                Glide.with(getApplicationContext())
                        .load(friendlyMessage.getDodataSlika())
                        .into(viewHolder.messageImageView);
                viewHolder.messageImageView.setVisibility(ImageView.VISIBLE);
                // viewHolder.messageTextView.setVisibility(TextView.GONE);
            }




            viewHolder.messengerTextView.setText(friendlyMessage.getAuthor());
            if (friendlyMessage.getSlika() == null) {
                viewHolder.messengerImageView.setImageDrawable(ContextCompat.getDrawable(TemaActivity.this,
                        R.drawable.ic_account_circle_black_36dp));
            } else {
                Glide.with(TemaActivity.this)
                        .load(friendlyMessage.getSlika())
                        .into(viewHolder.messengerImageView);
            }
            //recipient_sender.add(friendlyMessage.getRecipient_sender());
            // recipient.add(friendlyMessage.getRecipient());
            // sender.add(friendlyMessage.getSender());


        }


    };
    temaRecyclerView.setLayoutManager(mLinearLayoutManager);
    temaRecyclerView.setAdapter(mFirebaseAdapter);

1 个答案:

答案 0 :(得分:0)

即使我遇到了同样的问题。为了解决这个问题,我创建了一个原生的Recyclerview。

您的代码看起来像这样: -

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {

     //your object resource
     ArrayList<Object> resource;
     int itemResource;
     Context mContext;


public static class ViewHolder extends RecyclerView.ViewHolder {

    Object currentGEvent;
    ViewHolder holder;

    //your views here

    final TextView title;
    final TextView details;
    final ImageView gEventPicture;        
    final Context mContext;
    final ImageView profilePicture;

    View itemView;


    public ViewHolder(Context context, View itemView) {
        super(itemView);


        this.mContext = context;

        title = (TextView) itemView.findViewById(R.id.geventTitle);
        details = (TextView) itemView.findViewById(R.id.gEventsDetails);
        gEventPicture = (ImageView) itemView.findViewById(R.id.photoOf);
        profilePicture = (ImageView) itemView.findViewById(R.id.exampleProfilePic);


        this.itemView = itemView;
    }

    public void bindGEvent(final Object gEvent, ViewHolder viewHolder) {

       //Here you set text and the imageviews
    }

}

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

public CustomAdapter(Context context, int itemResource, ArrayList<Object> gEvents) {

    this.itemResource = itemResource;
    this.mContext = context;
    this.resource = gEvents;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(this.itemResource, parent, false);


    //view.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));

    return new ViewHolder(this.mContext, view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    Object gEvent = this.resource.get(position);
    holder.bindGEvent(gEvent, holder);

}

@Override
public int getItemViewType(int position) {
    return super.getItemViewType(position);
}

}