I have created RecyclerView example basing on Creating Lists and Cards guide. My adapter have a pattern implementation only for inflate the layout.
The problem is the poor scrolling performance. This in a RecycleView with only 8 items.
In some tests I verified that in Android L this problem does not occurs. But in the KitKat version the decreasing of performance is evident.
Best Answer
I've recently faced the same issue, so this is what I've done with the latest RecyclerView support library:
Replace a complex layout (nested views, RelativeLayout) with the new optimized ConstraintLayout. Activate it in Android Studio: Go to SDK Manager -> SDK Tools tab -> Support Repository -> check ConstraintLayout for Android & Solver for ConstraintLayout. Add to the dependencies:
If possible, make all elements of the RecyclerView with the same height. And add:
Use the default RecyclerView drawing cache methods and tweak them according to your case. You don't need third party library to do so:
If you use many images, make sure their size and compression are optimal. Scaling images may also affect the performance. There are two sides of the problem - the source image used and the decoded Bitmap. The following example gives you a hint how to decode аn image, downloaded from the web:
The most important part is specifying
inPreferredConfig
- it defines how many bytes will be used for each pixel of the image. Keep in mind that this is a preferred option. If the source image has more colors, it will still be decoded with a different config.Make sure onBindViewHolder() is as cheap as possible. You can set OnClickListener once in
onCreateViewHolder()
and call through an interface a listener outside of the Adapter, passing the clicked item. This way you don't create extra objects all the time. Also check flags and states, before making any changes to the view here.When data gets changed, try to update only the affected items. For example instead of invalidating the whole data set with
notifyDataSetChanged()
, when adding / loading more items, just use:From Android Developer Web Site :
But if you need to use it, maintain your items with unique ids:
Even if you do everything right, chances are that the RecyclerView is still not performing as smoothly as you would like.