Android Listview With Glide - Doubled Bitmaps After Load
Solution 1:
You're resuing list items (by definition of recycler view) which means that if an image was set to an item and you don't clear it, the image will remain there. So even though setText
changes the labels viewHolder.friendIcon
is not touched. The fix is really simple:
if (element.getPhoto() != null) {
Glide.with(getContext())...
} else {
Glide.clear(viewHolder.friendIcon); // tell Glide that it should forget this view
viewHolder.friendIcon.setImageResource(R.drawable.user_small); // manually set "unknown" icon
}
Also remove the drawable from the xml, or at least change to tools:src
which will help reducing the inflation time; the value is overwritten by Glide every time anyway.
To reduce complexity there's an alternative:
classFriendData {
// if you can't modify this class you can also put it in a `static String getPhotoUrl(Element)` somewherepublicvoidStringgetPhotoUrl() {
if (this.getPhoto() == null) returnnull;
String photo = S3ImageHandler.SMALL_PROFILE_ICON_PREFIX + this.getPhoto();
String url = String.format(S3ImageHandler.AMAZON_PROFILE_DOWNLOAD_LINK, photo);
return url;
}
}
and then replace the whole if (element.getPhoto() != null) {...}
with:
Glide.with(getContext())
.load(element.getPhotoUrl()) // this may be null --\.asBitmap() // |.diskCacheStrategy(DiskCacheStrategy.ALL) // |.placeholder(R.drawable.user_small) // |.fallback(R.drawable.user_small) // <--------------/.into(new BitmapImageViewTarget(viewHolder.friendIcon) { ... })
;
This will also result in proper behavior because even though there's no image url Glide will take care of setting something, see JavaDoc or source of fallback
.
As a sidenote also consider using CircleCrop
. Aside from caching benefits it would also support GIFs because you can remove the .asBitmap()
and the custom target.
Post a Comment for "Android Listview With Glide - Doubled Bitmaps After Load"