上下滚动列表后从 NetworkImageView 丢失图像 [英] Losing images from NetworkImageView after Scrolling list up-down

查看:32
本文介绍了上下滚动列表后从 NetworkImageView 丢失图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have a ListView that grabs news with images associated with it using Volley Json. At first it loads everything but when i scroll the list and come back, it sometimes shows images and sometimes not. After scrolling 2-3 times listview loses every image. Same procedure works perfectly with my test app (With Actionbar Tabs Only) so i don't know what is happening. Thanks in adavance for your time.

Note: I use MySingleton and LruBitmapCache from Developers site. My app's GUI is similar like Play Store i.e Navigation Drawer with Tabs. I searched problem like this but found no direct answer. Sorry if it is silly question to ask here.

If I use ViewHolder then that problem gets resolved but I am unable to remove NetworkImageView programmatically if URL is not there. I don't want to have placeholder image for row that doesn't have Image

Here is my layout file for row:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">

<com.android.volley.toolbox.NetworkImageView
    android:id="@+id/cat_thumbnail"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="8dp"
    />

<TextView
    android:id="@+id/cat_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16dp"
    android:layout_alignTop="@id/cat_thumbnail"
    android:layout_toRightOf="@id/cat_thumbnail"
    />

<TextView
    android:id="@+id/cat_paper"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/cat_title"
    android:layout_marginTop="1dip"
    android:layout_toRightOf="@+id/cat_thumbnail"
    android:textSize="15dip" />

Adapter class

private Activity activity;
private LayoutInflater inflater;
private List<NewsArticles> newsArticlesItems;
ImageLoader imageLoader;

public NewsAdapter(Activity activity, List<NewsArticles> newsArticlesItems)
{
    this.activity = activity;
    this.newsArticlesItems = newsArticlesItems;
}

@Override
public int getCount() {
    return newsArticlesItems.size();
}

@Override
public Object getItem(int location) {
    return newsArticlesItems.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int postion, View convertView, ViewGroup viewGroup) {

    if(inflater == null)
    {
        inflater = (LayoutInflater) activity.getSystemService(activity.LAYOUT_INFLATER_SERVICE);
    }
    if(convertView == null)
    {
        convertView = inflater.inflate(R.layout.cat_list_items, null);
    }
    if (imageLoader == null)
        imageLoader = MySingleton.getInstance(activity).getImageLoader();
    //Article image
    NetworkImageView thumbnail = (NetworkImageView) convertView.findViewById(R.id.cat_thumbnail);

    //Article title
    TextView title = (TextView) convertView.findViewById(R.id.cat_title);

    //Newspaper Name
    TextView newspaperName = (TextView) convertView.findViewById(R.id.cat_paper);

    //


    if(!URLUtil.isValidUrl(newsArticlesItems.get(postion).getThumbnailUrl()))
    {
        thumbnail.setVisibility(View.GONE);
    }
    /*if(newsArticlesItems.get(postion).getThumbnailUrl()=="")
    {
        thumbnail.setVisibility(View.GONE);
    }*/
    else
    {
        thumbnail.setImageUrl(newsArticlesItems.get(postion).getThumbnailUrl(), imageLoader);
    }

    title.setText(newsArticlesItems.get(postion).getTitle());
    newspaperName.setText(newsArticlesItems.get(postion).getNewspaperName());

    return convertView;
}

解决方案

Try like this in else block

else
    {
        thumbnail.setVisibility(View.VISIBLE);//add this
        thumbnail.setImageUrl(newsArticlesItems.get(postion).getThumbnailUrl(), imageLoader);
    }

这篇关于上下滚动列表后从 NetworkImageView 丢失图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆