Data disappears while moving Recyclerview scroll with Firebase

Asked

Viewed 129 times

2

Hello folks I am creating a chat with Firebase and listing with Recyclerview, to list the messages but I am with a problem is that when moving or scrolling Recyclerview some messages disappear, please help me to solve this!

Here is my code and the images of the result:

//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); 
        } 
    }

inserir a descrição da imagem aqui

onBindViewHolder Solved:

@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());
    }
}
  • Edit the question and add the Adapter code

  • ready this my Adapter there if it can say what I’m doing wrong I thank you very much!

1 answer

1


Your code is a little confusing to give an answer using it.

However, this type of problem occurs when in the onBindViewHolder() if you use one or more paths(if/Else) to do the bind.

This is because Recyclerview reuses (recycles, hence the name) views that uses for items.
Recyclerview creates only the number of views required for the number of items you can display simultaneously.
You will find that the problem only happens when the list has a number of items that requires you to use the scroll.

To make the process more efficient she uses the Standard View Holder.
An object is used Recyclerview.Viewholder where "are stored" the views.
In the method onCreateViewHolder() Viewholder is created and then passed to the method onBindViewHolder() when it is necessary to assign data to views.

The use of Viewholder, together with the two methods, allows views no longer used can be reused when necessary.
When the Viewholder is passed to the method onBindViewHolder(), if this refers to a view reused, it will come with its already filled attributes.
If they are not correctly assigned the new values, "strange things will happen", namely repeating the same values in different lines.

So it has to ensure that both in the if as in the else, Viewholder has all necessary values assigned.
For example, suppose the condition when it is true(block if) implies that a view is then hidden in the block else must make it visible. Otherwise view is reused will have the status it had on the original line.

See also: What is the purpose of the Recyclerview.Adapter class when using Recyclerview?

  • Good ramaral thanks for answering my question, What I’m trying to do is that if the id is iqual to my id it will show the ballad on the right side otherwise it will show me on the left side it shows the two messages normally but when it moves down and up some disappear. I will put but an image in the question.

  • you could give me a hint?

  • Notice when I have few conversations of everything ok but when there are many happens this.

  • Regarding the photos, if I understand the code well, there are two: one to the left and one to the right and only one is used at a time depending on the id. So whenever you use holder.Foto.setVisibility(view.GONE); have to use holder.Foto2.setVisibility(view.VISIBLE);(in the if block) and vice versa(in the Else block).

  • OK I’ll see here

  • Valew brother I edited as you had said and it worked out thank you very much!

  • Something so simple and I here bumping head thank you very much!

  • Things become simple when we know what is "behind". See the answer edition where I tried to explain why this happens.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.