立即在列表视图中加载图像

问题描述:

我很难理解在列表视图中加载图像的所有过程(在google和SO中)。但是,我设法让系统正常工作following example here立即在列表视图中加载图像

但加载的图像是每个视图,当我改变视图(滚动),它再次加载 [我想这是系统]。 还有一个更重要的问题是,你可以看到下面的图像,我在底部有11号图书的局部视图,因此图像被加载为顶级书籍(书#1)。如果我向下滚动然后再次回到顶部,那么Book#1加载默认灰色图像,如预期的那样...所以这个问题仅在第一次加载时发生

是否可以加载图像AT ONCE for后台任务中的所有列表,以便每次滚动时,都不会加载新图像 ?我如何解决下图中提到的问题?

我已经做了一些修改与一个SO成员的亲切帮助,包括一个复选框,但。以下是示例中更改的部分。 Rest类(ImageLoader,MemoryCache,Utils,FileCache)完全从上面的例子中复制而来。

enter image description here

底座适配器类 [从实施例改变]

public class MyList extends BaseAdapter { 

    static HashSet<String> selectedBooks = new HashSet<String>(); 
    private Activity activity; 
    private ArrayList<HashMap<String, String>> data; 
    private static LayoutInflater inflater=null; 
    public ImageLoader imageLoader; 

    public MyList(Activity a, ArrayList<HashMap<String, String>> d) { 
     activity = a; 
     data=d; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     imageLoader=new ImageLoader(activity.getApplicationContext()); 
    } 

    CompoundButton.OnCheckedChangeListener checkChangedListener = new CompoundButton.OnCheckedChangeListener(){ 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      String bookId = (String) buttonView.getTag(); 
      if(isChecked){ 
       selectedBooks.add(bookId); 
      }else{ 
       selectedBooks.remove(bookId); 
      } 
     } 
    }; 

    public static String[] getSelectedBooks(){ 
     return selectedBooks.toArray(new String [selectedBooks.size()]); 
    } 

    public int getCount() { 
     return data.size(); 
    } 

    public Object getItem(int position) { 
     //return position; 
     return data.get(position); 
    } 

    public long getItemId(int position) { 
     //return position; 
     return Long.parseLong(data.get(position).get(ShowList.LIST_KEY_ID)); 
    } 

    static class ViewHolder { 
     TextView title; 
     TextView writer; 
     TextView bookid; 
     CheckBox check; 
     ImageView thumb; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if(convertView==null){ 
      convertView = inflater.inflate(R.layout.listrow_row, null); 
      holder = new ViewHolder(); 
      holder.title = (TextView)convertView.findViewById(R.id.title); 
      holder.writer = (TextView)convertView.findViewById(R.id.writer); 
      holder.bookid = (TextView)convertView.findViewById(R.id.bookid); 
      holder.thumb = (ImageView)convertView.findViewById(R.id.thumb); 
      holder.check = (CheckBox)convertView.findViewById(R.id.check); 
      holder.check.setOnCheckedChangeListener(checkChangedListener); 
      convertView.setTag(holder); 
     }else{ 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     @SuppressWarnings("unchecked") 
     HashMap<String, String> book = (HashMap<String, String>) getItem(position); 
     holder.check.setTag(book.get(ShowList.LIST_KEY_ID)); 
     holder.title.setText(book.get(ShowList.LIST_KEY_NAME)); 
     holder.writer.setText(book.get(ShowList.LIST_KEY_WRITER)); 
     //holder.thumb.setImageResource(R.drawable.no_cover); 
     holder.bookid.setText(book.get(ShowList.LIST_KEY_ID)); 
     if(!book.get(ShowList.LIST_ISBN).trim().equals("")){ 
      Log.d("name","NAME: "+book.get(ShowList.LIST_KEY_NAME)); 
      Log.d("isbn","ISBN: "+book.get(ShowList.LIST_ISBN)); 
      imageLoader.DisplayImage(book.get(ShowList.LIST_ISBN), holder.thumb); 
     } 
     boolean bookSelected = false; 
     if(selectedBooks.contains(book.get(ShowList.LIST_KEY_ID))){ 
      bookSelected = true; 
     } 
     holder.check.setChecked(bookSelected); 
     return convertView; 
    } 
} 
+0

[这](http://stackoverflow.com/questions/541966/how-do-i-do-a-lazy-load-of-images-in-listview)将帮助你和所有人探索更多。 –

使用懒惰负载列表。

ImageLoader imageLoader =new ImageLoader(this); 
imageLoader.DisplayImage(imageURL, ImageView); 

网址:为您设定的图像https://github.com/thest1/LazyList

+2

我想我正在做同样的事情:-s – abdfahim

写else部分:

if(!book.get(ShowList.LIST_ISBN).trim().equals("")){ 
      Log.d("name","NAME: "+book.get(ShowList.LIST_KEY_NAME)); 
      Log.d("isbn","ISBN: "+book.get(ShowList.LIST_ISBN)); 
      imageLoader.DisplayImage(book.get(ShowList.LIST_ISBN), holder.thumb); 
     }else{ 
     holder.thumb.setImageResource(<DefaultImage>); 


} 
+0

没有工作..仍然是同样的问题 – abdfahim

+0

你有一些图像,你显示的情况下,书1 ?? ??或者它是默认的 –

+0

nope ..对于书#1,LIST_ISBN是“”(空),并且我已经为每行加载了xml的default_image。另外,有趣的是,如果我向下滚动一次,然后再往顶部滚动,那么Book#1具有默认的灰色图像,如预期的那样...所以这个问题仅在第一次加载期间发生 – abdfahim

解决这个问题只需通过改变layout_weight & layout_height = “FILL_PARENT” ......