如何在回收站视图中显示默认图像

时间:2015-08-25 13:39:21

标签: android android-recyclerview

我在android中使用Recycler View,我的Recycler视图包含一个ImageView和TextView,

当我从JSON响应中获取数据时,Recycler视图仅显示textView,直到网络图像未完全加载,我想在ImageView中显示默认图像(例如,来自drawable文件夹的图像),直到网络图像加载,一旦网络图像加载它替换默认图像,我怎么能实现这个?? ...

这是我的适配器代码:

@Override
public ViewHolderVideos onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = layoutInflater.inflate(R.layout.videos, parent,false);
    ViewHolderVideos viewHolder = new ViewHolderVideos(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolderVideos holder, int position) {

    VideoData currentVideos = listVideos.get(position);
    holder.video_title.setText(currentVideos.getTitle());
    String urlThumbnail = currentVideos.getUrlThumbnail();
    loadImages(urlThumbnail, holder);
}

public void setClickListener(ClickListener clickListener){
    this.clickListener = clickListener;
}

private void loadImages(String urlThumbnail, final ViewHolderVideos holder) {
    if ((!urlThumbnail.equals(Constants.NA))){
        imageLoader.get(urlThumbnail, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate){
                holder.video_thumbnail.setImageBitmap(response.getBitmap());
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                holder.video_thumbnail.setImageResource(R.mipmap.ic_launcher);
            }
        });
    }
    else {
        holder.video_thumbnail.setImageResource(R.mipmap.ic_launcher);
    }
}

1 个答案:

答案 0 :(得分:1)

如果你使用凌空,你可以这样做:

ImageListener listener = ImageLoader.getImageListener(imageView,  
        R.drawable.default_image, R.drawable.failed_image); 
imageLoader.get(url, listener);