Richard Mannion
Richard Mannion

Reputation: 289

Android, loading images efficiently in a list adapter

I am writing an android app. I have an ArrayAdapter which I am using to display a list of items where part of each item is an image. I am downloading the images asynchronously in the adapers getView() method and I am using a cache to keep it as efficient as possible and stop too many re-downloads. The problem I am facing is that the getView() method for each item can be called by android as many times as it needs to. It is often called faster than the image can actually download and so the same image can be queued many times for download before it even gets into the cache. Is there a best practice for how to prevent the images from being queued for download repeatedly? It just seems like a big waste of mobile data.

Upvotes: 1

Views: 184

Answers (2)

Shmuel
Shmuel

Reputation: 3916

The accepted best practice that is recommend by Google for downloading and displaying images in a list or recyclerview is to use the Glide library

https://github.com/bumptech/glide

The api looks like this

Glide.with(context) .load(url) .centerCrop() .placeholder(R.drawable.loading_spinner) .crossFade() .into(myImageView);

Upvotes: 0

Rohit Singh
Rohit Singh

Reputation: 18192

If you want to use third party image downloader library then you can use Picasso
Its actullay pretty neat and simple to use. It does caching for your app.
You can refer this tutorial.
Happy learning :)

Upvotes: 1

Related Questions