Recyclerview NetworkImageView (volley) doesn't show up

江枫思渺然 提交于 2019-12-11 06:07:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!