diff options
author | Aurash Mahbod <aurash@google.com> | 2013-05-20 14:16:09 -0700 |
---|---|---|
committer | Aurash Mahbod <aurash@google.com> | 2013-05-20 14:16:09 -0700 |
commit | b25a6e0f4d3bfdeddfc10e8f5518bab5c16634fd (patch) | |
tree | af74446de26eafa8776de1d03b588399bf432d61 /src | |
parent | c6b2aaa5641f0f1c03b8126e1b617eb2f9b45b18 (diff) | |
download | volley-b25a6e0f4d3bfdeddfc10e8f5518bab5c16634fd.tar.gz |
Don't requestLayout in layout pass in NIV
Diffstat (limited to 'src')
-rw-r--r-- | src/com/android/volley/toolbox/NetworkImageView.java | 41 |
1 files changed, 37 insertions, 4 deletions
diff --git a/src/com/android/volley/toolbox/NetworkImageView.java b/src/com/android/volley/toolbox/NetworkImageView.java index ca01286..0d1d6be 100644 --- a/src/com/android/volley/toolbox/NetworkImageView.java +++ b/src/com/android/volley/toolbox/NetworkImageView.java @@ -20,7 +20,9 @@ import android.text.TextUtils; import android.util.AttributeSet; import android.widget.ImageView; +import com.android.volley.VolleyError; import com.android.volley.toolbox.ImageLoader.ImageContainer; +import com.android.volley.toolbox.ImageLoader.ImageListener; /** * Handles fetching an image from a URL as well as the life-cycle of the @@ -74,7 +76,7 @@ public class NetworkImageView extends ImageView { mUrl = url; mImageLoader = imageLoader; // The URL has potentially changed. See if we need to load it. - loadImageIfNecessary(); + loadImageIfNecessary(false); } /** @@ -95,8 +97,9 @@ public class NetworkImageView extends ImageView { /** * Loads the image for the view if it isn't already loaded. + * @param isInLayoutPass True if this was invoked from a layout pass, false otherwise. */ - private void loadImageIfNecessary() { + private void loadImageIfNecessary(final boolean isInLayoutPass) { int width = getWidth(); int height = getHeight(); @@ -131,7 +134,37 @@ public class NetworkImageView extends ImageView { // The pre-existing content of this view didn't match the current URL. Load the new image // from the network. ImageContainer newContainer = mImageLoader.get(mUrl, - ImageLoader.getImageListener(this, mDefaultImageId, mErrorImageId)); + new ImageListener() { + @Override + public void onErrorResponse(VolleyError error) { + if (mErrorImageId != 0) { + setImageResource(mErrorImageId); + } + } + + @Override + public void onResponse(final ImageContainer response, boolean isImmediate) { + // If this was an immediate response that was delivered inside of a layout + // pass do not set the image immediately as it will trigger a requestLayout + // inside of a layout. Instead, defer setting the image by posting back to + // the main thread. + if (isImmediate && isInLayoutPass) { + post(new Runnable() { + @Override + public void run() { + onResponse(response, false); + } + }); + return; + } + + if (response.getBitmap() != null) { + setImageBitmap(response.getBitmap()); + } else if (mDefaultImageId != 0) { + setImageResource(mDefaultImageId); + } + } + }); // update the ImageContainer to be the new bitmap container. mImageContainer = newContainer; @@ -140,7 +173,7 @@ public class NetworkImageView extends ImageView { @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); - loadImageIfNecessary(); + loadImageIfNecessary(true); } @Override |