问题
I am using RecyclerView
and volley's NetworkImageView
to render images once they are downloaded. The view consists of an author image, some text fields and a picture. Following is the code snippet to populate the view:
// vh is the viewholder
vh.picture.setDefaultImageResId(R.drawable.default_image);
vh.picture.setImageUrl(post.getImageUrl(), mImageLoader);
The problem I am facing is when scrolling, out of say 20 images, mostly ~18 show up. I see from the logs that all images are downloaded and are in the cache, but some are not rendered. Even the default image is not displayed for those views. If the view is invalidated (scroll up and down again), the images show up.
Funny thing is, for the views where the picture is not displayed, even the author pic is not displayed, even if I can see the same author pic in a post just above it. Its as if the entire view has a problem displaying images.
Is there any way to call invalidate()
or postInvalidate()
on NetworkImageView
manually once the images are downloaded? Or any other ideas?
回答1:
This was also asked here. I finally got around to this problem by not using NetworkImageView at all. I started using the regualar ImageView, am still fetching the images through volley through a custom image request and onResponse() applying the image on the view. This seems to work pretty well.
public void getImage(String url, final ImageView v) {
if (TextUtils.isEmpty(url)) return; // don't fetch a null url
ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
v.setImageBitmap(response);
}
}, 0, 0, null, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error- " + error.getMessage());
}
});
mRequestQueue.addToRequestQueue(imageRequest);
}
来源:https://stackoverflow.com/questions/40916054/recyclerview-networkimageview-volley-doesnt-show-up