0

How should we handle the adapter position inside a delayed(probably) listener callback of Glide when the size of the list is changed before the ballcack is received?

Error: Caused by: java.lang.IndexOutOfBoundsException: Index: 3, Size: 3

I have implemented Glide with onResourceReady() listener, in the recycler view adapter

int position = holder.getAbsoluteAdapterPosition();
Glide.with(context)
                    .load(Utils.getResponsiveImage(imgUrlS))
                    .error(Glide.with(context).load(imgUrlS)
                            .listener(new RequestListener<Drawable>() {
                                @Override
                                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                                    testViewHolder.pbLoadImage.setVisibility(View.GONE);
                                    ((TestViewHolder) holder).ivView.setScaleX(1f);
                                    ((TestViewHolder) holder).ivView.setScaleY(1f);
                                    return false;
                                }

                                @Override
                                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                                    testViewHolder.pbLoadImage.setVisibility(View.GONE);
          THIS CAUSES ERROR---->           if (blankFragments.get(position).isEnabled && blankFragments.get(position).url == null) {
                                       //Some work here
                                    } else {
                                        ((TestViewHolder) holder).ivView.setScaleX(1f);
                                        ((TestViewHolder) holder).ivView.setScaleY(1f);
                                    }
                                    return false;
                                }
                            })

I have also tried the holder.getBindingAdapterPosition() but then it gives Caused by: java.lang.IndexOutOfBoundsException: Index: 4, Size: 3 error.

The list of this adapter is depending on Google Map bounds, so if the user moves the map it will add/remove the items in the adapter, so the crash happens sometimes when the image is still loading and I move the map.

Is there anything I'm missing or any good practice to handle such a situation?

0 Answers0