Facebook Twitter Skype Linkedin Youtube Google Behance Dribbble
Menu
RecyclerView Tips and Recipes
Serhii Yaremych

RecyclerView is a flexible view for providing a limited window into a large data set. RecyclerView is an evolutionarily version of Listview – one of the most important widgets used in Android development. Android apps using Listview for implementing newsfeed or contact lists will likely face performance issues or unnecessary lags when the user scrolls the view fast. But RecyclerView has an improved performance, and it provides a fluid scroll experience for the user.

This article is not about the RecyclerView; there are thousands of other tutorials that will help you use it. This topic assumes you know how to work with RecyclerView.

I will give you useful tips and important rules you should follow and what you should never do when working with RecyclerView.

Possible problems and ways to avoid them

1. Death #1:

Can’t recycle views properly.

How to fix:

Do not animate views inside viewholder (e.g., calling itemView.animate().ItemAnimator) is the ONLY component that can animate views.

death#1

2. Death #2

Too many ViewHolders of the same type.

How to fix:

Granular adapter updates(e.g. notifyItemChanged(4))

Do not animate crossfade for all views

Never ever use notifyItemRangeChanged() this way notifyItemRangeChanged(0, getItemsCount())

Use DiffUtil to manage adapter updates

death#2

3. Death #3

onBindViewHolder position != final

Never ever set View.OnClickListener inside onBindViewHolder(RecyclerView.ViewHolder holder, int position)

death#3

Implement View.OnClickListener on RecyclerView.ViewHolder class and set itemView.setOnClickListener(this) in the constructor:

class ColorViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    ColorViewHolder(View itemView, ItemClickListener clickListener) {
        super(itemView);
        itemView.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        removeAtPosition(getAdapterPosition())
    }}

Follow these simple rules, and you will have no problems with the RecyclerView.

Easy way to use dataset change animations.

1. Use notifyItemChanged(), notifyItemRangeChanged(), notifyItemInserted(), notifyItemMoved(), notifyItemRemoved(), and others notifyItem*() to visualize dataset change with animations.

2. Use DiffUtil. It handles all change calculations for you and dispatches them to the adapter.

3. Use setHasStableIds(true) with getItemId(int), and RecyclerView will automatically handle all animations on simple call notifyDataSetChanged().

Here are some animations for better understanding:

animation 1     animation 2

Performance tips

If you want to achieve smooth scrolling, you should not forget about the next simple rules:

You have only 16ms to do all your work per frame, enable Profile GPU Rendering option to monitor rendering performance.

Try to keep item layout structure simple.

Try to avoid deep layout hierarchies, use HierarchyViewer to find performance bottleneck.

Avoid overdraw issue, use system tools to monitor it.

Do not set long text into TextView, it causes text line wrap heavy calculations. Set max lines count with text end ellipsis.

Use LayoutManager.setItemPrefetchEnabled() for nested RecyclerViews to improve rendering performance.

Untitled-1

Click to watch the smooth scroll

As you can tell, RecyclerView is a powerful widget. With it, you can achieve almost any result you want. Use it at least for the nice animations.


Also published on Medium.

comments powered by HyperComments

Read also

All articles
comments powered by HyperComments