Android: RecyclerView content messed up after scrolling

Swisyn picture Swisyn · Apr 17, 2015 · Viewed 44.4k times · Source

I'm using RecyclerView to display a list of marks, and each mark of the value is shown as a CardView. But some contents of the cards are lost after the scrolling the RecyclerView down and scrolling back, as shown in the two screenshots below. The contents in the red rectangle is lost after scrolling.

BEFORE THE SCROLLING; enter image description here

AFTER THE SCROLLING; enter image description here

I'm wondering whether or not it's a bug of RecyclerView and find no solution after Googling for it.

All views are invisible except the title, their visibilities are depends on the mark's value.

Does anyone know why this would happen?

Answer

aphoe picture aphoe · Jul 26, 2016

After battling with this same issue for about 24 hours, I found a solution that worked for me. The key was using the setIsRecyclable() method of RecyclerView.ViewHolder class.

Here is a section of my onBindViewHolder() code.

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    final DataSource dataSource = dataSourceList.get(position);

    holder.setIsRecyclable(false);

    holder.name.setText(dataSource.getName());
    holder.active.setChecked(dataSource.getActive());

    String logoStr = dataSource.getLogo();

    //Logo
    /**
     * Do all the logo insertion stunts here
     */
    /**
     * Register the changes to the Switch
     */
    holder.active.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
            dataSource.setActive(isChecked);
        }
    });
}