title: How to avoid IllegalStateException When calling notifyDataSetChanged?
body: <p>I have a horizontal recycler view called <code>imageRecyclerView</code> where each column consists solely of one <code>ImageView</code>. The adapter class for the recycler view is called <code>ImageAdapter</code>. The dataset of <code>imageRecyclerView</code>'s adapter is an <code>ArrayList</code> of strings that contain URLs. The <code>ArrayList</code> is called <code>currentImageURLs</code>.</p>
  
  <p>Note - the purpose of all this code is to load images into <code>imageRecyclerView</code> one at a time.</p>
  
  <p>In the <code>onBindViewHolder</code> method of my <code>ImageAdapter</code> class I have the following code. This code loads an image from the adapter's dataset into an <code>ImageView</code>. The code also waits until the image has been loaded into the <code>ImageView</code> before calling <code>recursivelyLoadURLs</code> to add a new item to the adapter's dataset.  </p>
  
  <pre><code>    Glide.with(context)
              //items[[position] is a string that contains a URL 
              .load(items[position])
              .listener(object : RequestListener&lt;Drawable&gt; {
                  override fun onLoadFailed(e: GlideException?, model: Any?, target: Target&lt;Drawable&gt;?, isFirstResource: Boolean): Boolean {
                      holder.progressBar.visibility = View.GONE
                      context.recursivelyLoadURLs()
                      return false
                  }
  
                  override fun onResourceReady(resource: Drawable?, model: Any?, target: Target&lt;Drawable&gt;?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                      holder.progressBar.visibility = View.GONE
                      context.recursivelyLoadURLs()
                      return false
                  }
  
              })
              .apply(RequestOptions()
                      .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC))
              .into(holder.imageView)
  </code></pre>
  
  <p>The method <code>recursivelyLoadURLs</code> is in the parent Activity class. In essence, what this method does is</p>
  
  <p>1] add another URL to <code>imageRecyclerView</code>'s adapter's dataset and</p>
  
  <p>2] call <code>notifyDataSetChanged</code> so <code>imageRecyclerView</code> updates:</p>
  
  <pre><code>fun recursivelyLoadURLs() {
  
  
      if (currentImageURLs.size &gt;= targetImageURLs.size) {
          return
      }
  
      currentImageURLs.add(targetImageURLs[currentImageURLs.size])
  
      //The code crashes on this line
      mainview.imageRecyclerView.adapter.notifyDataSetChanged()
  
  
  }
  </code></pre>
  
  <p>However, the code is crashing with the exception <code>java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling</code> on the line labeled in the above code sample.</p>
  
  <p>I suspect this is happening because <code>imageRecyclerView</code> is still computing a layout when <code>notifyDataSetChanged</code> is being called.</p>
  
  <p>How can I delay calling <code>context.recursivelyLoadURLs</code> until the layout has finished being computed?</p>
  
  <p>Or, how can I add a delay inside <code>recursivelyLoadURLs</code>, so that <code>notifyDataSetChanged</code> is only called once <code>imageRecyclerView</code> has finished computing its new layout?</p>