我很難理解在列表視圖中加載圖像的所有過程(在google和SO中)。但是,我設法讓系統正常工作following example here。立即在列表視圖中加載圖像
但加載的圖像是每個視圖,當我改變視圖(滾動),它再次加載
[我想這是系統]。 還有一個更重要的問題是,你可以看到下面的圖像,我在底部有11號圖書的局部視圖,因此圖像被加載爲頂級書籍(書#1)。如果我向下滾動然後再次回到頂部,那麼Book#1加載默認灰色圖像,如預期的那樣...所以這個問題僅在第一次加載時發生
是否可以加載圖像AT ONCE for後臺任務中的所有列表,以便每次滾動時,都不會加載新圖像
?我如何解決下圖中提到的問題?
我已經做了一些修改與一個SO成員的親切幫助,包括一個複選框,但。以下是示例中更改的部分。 Rest類(ImageLoader,MemoryCache,Utils,FileCache)完全從上面的例子中複製而來。
底座適配器類 [從實施例改變]
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;
}
}
[這](http://stackoverflow.com/questions/541966/how-do-i-do-a-lazy-load-of-images-in-listview)將幫助你和所有人探索更多。 –