Hi everyone, I am creating a chat with Firebase and listing with RecyclerView, to list the messages but I have a problem is that when moving or scrolling the RecyclerView some messages disappear, please help me solve this!
Here is my code and the result images:
//onCreate
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI]
Model.clear();
getAllTask(dataSnapshot);
// ...
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w("TAG", "loadPost:onCancelled", databaseError.toException());
// ...
}
};
mData.child("UTILADMIN").addValueEventListener(postListener);
//end onCreate
private void getAllTask(DataSnapshot dataSnapshot){
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
ModelMessage modelMessage = singleSnapshot.getValue(ModelMessage.class);
Model.add(modelMessage);
Adapters adaptador = new Adapters(this, Model, mImageLoader, mUser.getUid());
adaptador.notifyItemInserted(Model.size() - 1);
recyclerView.setAdapter(adaptador);
linearLayoutManager.scrollToPosition(Model.size() - 1);
}
}
onBindViewHolder Resolved:
@Override
public void onBindViewHolder(ViewHolders holder, int position) {
if(Mensagem.get(position).getId().equalsIgnoreCase(this.Id)){
holder.chat1.setVisibility(view.GONE); //RelativeLayout chat1 INVISIVEL
holder.chat2.setVisibility(view.VISIBLE);//RelativeLayout chat2 VISIVEL
holder.Nome2.setText(Mensagem.get(position).getNome());
holder.TextMensagem2.setText(Mensagem.get(position).getMensagem());
}else{
holder.chat1.setVisibility(view.VISIBLE);//RelativeLayout chat2 VISIVEL
holder.chat2.setVisibility(view.GONE);//RelativeLayout chat1 INVISIVEL
holder.Nome1.setText(Mensagem.get(position).getNome());
holder.TextMensagem1.setText(Mensagem.get(position).getMensagem());
}
}