【问题标题】:Loading images in Listview at once一次在 Listview 中加载图像
【发布时间】:2013-12-11 04:31:06
【问题描述】:

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

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

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

在一位 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;
    }
}

【问题讨论】:

  • This 将帮助您和所有人探索更多。

标签: android listview


【解决方案1】:

使用延迟加载列表。

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

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

【讨论】:

  • 我想我也在做同样的事情:-s
【解决方案2】:

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


}

【讨论】:

  • 你有一些你在 book-1 的情况下展示的图片吗?或者它是默认的
  • nope .. 对于 Book#1,LIST_ISBN 是“”(空),并且我已经在 xml 中为每一行加载了 default_image。另外,有趣的是,如果我向下滚动一次然后再次回到顶部,那么 Book#1 就会像预期的那样具有默认的灰色图像......所以这个问题只在第一次加载时发生
【解决方案3】:

通过改变 layout_weight & layout_height= "fill_parent" ...

解决了这个问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多