立即在Listview中加载图像

时间:2013-12-11 04:31:06

标签: android listview

我很难理解在列表视图中加载图片的所有过程(在google和SO中)。但是,我设法让系统运行following example here

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

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

我已经通过一个SO成员的帮助进行了一些修改,但是包含了一个复选框。以下是示例中更改的部分。其余类(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;
    }
}

3 个答案:

答案 0 :(得分:0)

使用延迟加载列表。

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

网址:https://github.com/thest1/LazyList

答案 1 :(得分:0)

为您设置的图片写下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>);


}

答案 2 :(得分:0)

仅通过更改layout_weight&amp;来解决问题layout_height =“fill_parent”......

相关问题