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