问题
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;
}
回答1:
Try like this in else block
else
{
thumbnail.setVisibility(View.VISIBLE);//add this
thumbnail.setImageUrl(newsArticlesItems.get(postion).getThumbnailUrl(), imageLoader);
}
来源:https://stackoverflow.com/questions/30850315/losing-images-from-networkimageview-after-scrolling-list-up-down