RecyclerView was introduced with Android LollipopCodeware 

How to prevent Android’s RecyclerView from recycling views

The RecyclerView widget, alongside the CardView widget, was introduced in Android API 21 (Lollipop). RecyclerView is a more flexible and more advanced version of ListView, according to Android developers’ website. But don’t read flexible as easier to implement. It’s cool though. yeah, it’s ListView on steroids.

One of its key feature is that it recycles views that are out of device screen. It is a cool feature but it’s not always a good thing. There are times you don’t really want to recycle views. Or when recycling messes up your display.

To overcome that, you have to take advantage of the setIsRecyclable() of the RecyclerView.ViewHolder class. Take a look at the onBindViewHolder() method to see how I solved implemented this.

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

You can argue that this is going against what RecyclerView was designed to do, but then remember that Google themselves must have considered that recycling might not always be a desired result, hence they created a shortcut.

Hope this works for you.

 

P.S.: If you have any issues or questions, don’t hesitate to ask questions via the comments below. I will try to answer as many as I can. Well, you can also talk about anything related too. No, no spamming please

P.P.S.: if you want to learn how to work with RecyclerView, I recommend a very good tutorial on AndroidHive.info

Afolabi 'aphoe' Legunsen on FacebookAfolabi 'aphoe' Legunsen on GithubAfolabi 'aphoe' Legunsen on GoogleAfolabi 'aphoe' Legunsen on LinkedinAfolabi 'aphoe' Legunsen on TwitterAfolabi 'aphoe' Legunsen on Youtube
Afolabi 'aphoe' Legunsen
Software Project Lead at itquette solutions
I'm a geek who desires to communicate with humans.

If you understand what I wrote and are not a fellow geek, then I've succeeded at having normal human conversations.

Related posts

Leave a Comment

%d bloggers like this: