Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent the same url and imageview being fetched many times at the same time #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/com/fedorvlasov/lazylist/ImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public ImageLoader(Context context){

final int stub_id=R.drawable.stub;
public void DisplayImage(String url, ImageView imageView)
{
{ //prevent the same url and imageview being fetched many times at the same time

imageViews.put(imageView, url);
Bitmap bitmap=memoryCache.get(url);
if(bitmap!=null)
Expand Down Expand Up @@ -119,6 +120,8 @@ public PhotoToLoad(String u, ImageView i){
}
}

private Map<Long, Boolean> RunningTask = Collections
.synchronizedMap(new WeakHashMap<Long, Boolean>());
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad){
Expand All @@ -129,7 +132,12 @@ class PhotosLoader implements Runnable {
public void run() {
if(imageViewReused(photoToLoad))
return;
long key = (long) photoToLoad.imageView.hashCode() + photoToLoad.url.hashCode();
Boolean isRunning = RunningTask.get(key);
if(isRunning != null && isRunning.equals(true)) return;
RunningTask.put(key, true);
Bitmap bmp=getBitmap(photoToLoad.url);
RunningTask.remove(key);
memoryCache.put(photoToLoad.url, bmp);
if(imageViewReused(photoToLoad))
return;
Expand Down