RecyclerView shuffles items and changing values on scrolling fast, Android

2 minute(s) reading

Problem

RecyclerView list gets shuffled and changing position of items inside the adapter on scrolling fast in Android application.

Final output

Solved issue, RecyclerView will scroll smoothly and list items would remain on the same position even after scrolling fast.

What is RecyclerView?
Android introduced RecyclerView widget. RecyclerView is flexible and efficient version of ListView. It is an container for rendering larger data set of views that can be recycled and scrolled very efficiently.

Whenever you are working with a RecyclerView. You may face an issue that item shuffles a lot and it’s changing the position of list items during fast scrolling. 

You get this issue when you scroll down and then back to the top and you see that items were changed for other items in the list and adapter shows the wrong data.

Then if you scroll again, the items again has changed and content are messed up while scrolling quickly.

So this problem can be quite hard to debug and the question arises. Is there any way to prevent this effect?

The answer is YES 🙂 

So here is the Solution for this snag.

Solution

The solution is very simple you Just have to override two methods in your Adapter class and it will solve your issue.

The reason is because some other overridden methods need to call these override methods while rendering the list items. 

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
    return position;
}
Add Override methods in Adapter class

Alternative Solution

You might set setIsRecyclable(false) while creating ViewHolder. But this would defeats the purpose of having it and RecylerView will not properly recycle the list items.

A big advantage of RecyclerView is that it reuses ViewHolder instances. 

So when the user scrolls and new items are getting visible, instead of creating new views, previous ViewHolders which are not visible anymore are recycled and bound to newly visible items.

And RecyclerView will handle each ViewHolder as not reusable for a new item because it has a different type. So the “recycle feature” of RecyclerView is basically disabled by setting setIsRecyclable as false.

Leave a Reply