Because there are many people on the network have written recyclerView performance optimization, most of them are good, but there is a point many people did not notice, I record here.

When data is displayed in recyclerView, many people prefer to use the traversal form in onBindViewHolder, which is a slower way, if there is a lot of adding data. Data. ForEach is slower than simple var a = data[position]. The larger the data, the slower the traversal. So the best way to write it is var a = data[position].

Glide. With (mContext).Load (path). Into (holder.imageView). It takes 30ms to display an image (depending on the size of the image, of course), but less than 10ms using the following method

GlobalScope.launch {
            val bitmap = Glide.with(context).asBitmap().load(url).submit().get()
            withContext(Dispatchers.Main) { setImageBitmap(bitmap) }
        }
Copy the code

Glide. With (mContext).load(path).into(holder.imageView) this way is also better than recyclerView to add data

GlobalScope.launch {
            val bitmap = Glide.with(context).asBitmap().load(url).submit().get()
            withContext(Dispatchers.Main) { setImageBitmap(bitmap) }
        }
Copy the code

More time-consuming. Personally, I like layout optimization very much. When I call RecyclerView, I don’t like to use addItemDecoration to set the interval. I usually use LayoutParams, the outermost layer of item, to set the interval. Because the UI set up in this way can meet almost any phone adaptation.