Android Fresco Optimize Performance for RecyclerView

RecylerView

Consider RecyclerView.hasFixedSize

RecyclerView can perform several optimizations if it can know in advance that RecyclerView's size is not affected by the adapter contents. RecyclerView can still change its size based on other factors (e.g. its parent's size) but this size calculation cannot depend on the size of its children or contents of its adapter (except the number of items in the adapter).

If your use of RecyclerView falls into this category, set this to true. It will allow RecyclerView to avoid invalidating the whole layout when its adapter contents change.

list = ... // RecyclerViewlist.setHasFixedSize(true)

Enable RecyclerView.Adapter.setHasStableIds

class LocalAdapter() : RecyclerView.Adapter<LocalAdapter.ViewHolder>() {    private var items = ...    init {        setHasStableIds(true)    }    override fun getItemId(position: Int): Long {        // return items[position].id        return items[position].hashCode().toLong()    }}

Fresco

Enable Fresco downsampling

val config = ImagePipelineConfig.newBuilder(context)    .setDownsampleEnabled(true)    .build()Fresco.initialize(context, config)

Use ResizeOptions

// I am using GridLayout with 3 columnsval estimatedImageSize = Resources.getSystem().displayMetrics.widthPixels / layoutManager.spanCountval adapter = LocalAdapter(viewModel, estimatedImageSize)
class LocalAdapter(val estimatedImageSize: Int) : RecyclerView.Adapter<LocalAdapter.ViewHolder>() {    private var resizeOptions: ResizeOptions? = null    private var items = ...    override fun onBindViewHolder(holder: ViewHolder, position: Int) {        val item = items[position]        // try to get exact size at runtime when DraweeView is rendered        val localResizeOptions = if (resizeOptions == null) {            holder.draweeView.doOnLayout {                resizeOptions = ResizeOptions(holder.draweeView.width, holder.draweeView.height)            }            ResizeOptions(estimatedImageSize, estimatedImageSize)        }        else resizeOptions        val request =              ImageRequestBuilder.newBuilderWithSource(item.uri)                  .setResizeOptions(localResizeOptions)                  .build()        holder.draweeView.setImageRequest(request)    }}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.