Data disappear when scrolling scroll RecyclerView with Firebase

2

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());
    }
}
    
asked by anonymous 17.11.2017 / 13:50

1 answer

1

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

However, this type of problem occurs when in onBindViewHolder() one or more paths (if / else) are used to bind .

This happens because RecyclerView reuses (recycles, hence the name) the views that it uses for the items. RecyclerView creates only the number of views required for the number of items it can display simultaneously.
You will see that the problem only happens when the list has a number of items that require scroll .

To make the process more efficient, it uses the Default View Holder . A RecyclerView.ViewHolder object is used where views are stored. In the onCreateViewHolder() method the ViewHolder is created and then passed to the onBindViewHolder() method when it is necessary to assign data to views .

Using ViewHolder , together with both methods, allows views to be reused when needed. When the ViewHolder is passed to the onBindViewHolder() method, if it refers to a view reused, it will come with its attributes already filled. If they are not correctly assigned the new values, "strange things will happen," namely repeating equal values on different lines.

So, you have to make sure that both the if and else , the ViewHolder has all the required values assigned. For example, suppose that the condition when it is true (block if ) implies that a view is hidden then in block else must make it visible. Otherwise when this view is reused it will have the state it had on the original line.

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

    
17.11.2017 / 15:08