title: Android RecyclerView freezes UI on a notifyDataSetChanged call
body: <p>I know there have been many questions regarding this topic, but I still could not find a solution.</p>
  <p>I have an application, that loads and shows images (just like a regular gallery app on a smartphone)</p>
  <p>I load all the images in the background with coroutines - no problem at this stage of the process.</p>
  <p>The problem occurs when I notify my adapter about the data. notifyDataSetChanged() call freezes the UI for a couple of moments.</p>
  <p>We are talking about relatively large grids (1000 + photos for example)</p>
  <p>Almost all the answers in other threads suggest, that I should check for which items changed
  and call notifyItemChanged(index) rather than call notifyDataSetChanged() or use DiffUtil or something similar. I understand all of this, but at the RecyclerView initialization (when the application loads) there is no existing data, so I don't see other options as calling notifyDataSetChanged().</p>
  <p>Adapter code:</p>
  <pre><code>class PhotosAdapter(private val mFragment: PhotosFragment, private val mListener: ClickListener) : RecyclerView.Adapter&lt;PhotoViewHolder&gt;() {
  
      private var photos: List&lt;PhotoFile&gt;? = null
      
      init {
          mFragment.mPhotosViewModel.photos.observe(mFragment, Observer {
              it?.let {
                  photos = it
                  notifyDataSetChanged()
              }
          })
      }
  
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoViewHolder {
          return PhotoViewHolder.from(parent)
      }
  
      override fun onBindViewHolder(holder: PhotoViewHolder, position: Int) = holder.bind(
          position, photos!!.get(position), mFragment, mListener
      )
  
      override fun getItemCount(): Int = photos?.size ?: 0
  }
  
  class PhotoViewHolder private constructor(private val mBinding: PhotoGridItemBinding) : RecyclerView.ViewHolder(mBinding.root) {
  
      companion object {
          fun from(parent: ViewGroup) : PhotoViewHolder {
              val layoutInflater = LayoutInflater.from(parent.context)
              val binding = PhotoGridItemBinding.inflate(layoutInflater, parent, false)
              return PhotoViewHolder(binding)
          }
      }
  
  
      fun bind(index: Int, photo: PhotoFile, photosFragment: PhotosFragment, listener: ClickListener) {
          mBinding.index = index
          mBinding.photo = photo
          mBinding.viewHolder = this
          mBinding.listener = listener
          
          Glide.with(photosFragment).load(photo.uri)
              .thumbnail(0.1f).into(mBinding.photoGridItemThumbnail)
  
          mBinding.executePendingBindings()
      }
  }
  </code></pre>
  <p>Regards, L</p>