Android的ArrayAdapter和放大器;缓慢的ListView [英] Android ArrayAdapter & ListView slowness

查看:310
本文介绍了Android的ArrayAdapter和放大器;缓慢的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要显示和放大器;在我的ListView我创建了一个自定义的ArrayAdapter载入图像,但它实在是太慢了。我每次滚动列表再次加载图像。

To display & load images in my ListView I've created a custom ArrayAdapter, however it is really slow. Every time I scroll through the list it loads the images again.

有一个缓存机制,将缓存我的意见,我不需要每次都重新填充呢?我了解ViewHolder方法,但是这仍然是慢还是我只是用错了?

Is there a caching mechanism which will cache my Views so I don't need to refill them every time? I know about the ViewHolder method, however this still is slow or am I just using it wrong?

我的code:

public class ImageArrayAdapter extends ArrayAdapter<HashMap<String, String>> {

private int resourceId = 0;
private LayoutInflater inflater;

public ImageArrayAdapter(Context context, int resource,
        List<HashMap<String, String>> objects) {
    super(context, 0, objects);

    this.resourceId = resource;
    this.inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public static class ViewHolder {
    public TextView title;
    public TextView text;
    public WebImageView image;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    ViewHolder holder;

    if (view == null) {
        view = inflater.inflate(resourceId, parent, false);

        holder = new ViewHolder();

        holder.text = (TextView) view.findViewById(R.id.item_text);
        holder.image = (WebImageView) view.findViewById(R.id.item_image);
        holder.title = (TextView) view.findViewById(R.id.item_title);

        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    HashMap<String, String> item = (HashMap<String, String>) getItem(position);

    holder.text.setText(item.get("text"));
    holder.title.setText(item.get("title"));
    holder.image.setImageUrl(item.get("image"));
    holder.image.loadImage();

    return view;
}

}

推荐答案

您可以将您的位图在LRU基础上SoftReferences(最近最少使用)高速缓存。

You can store your Bitmaps in a LRU (Least Recently Used) cache based on SoftReferences.

那种缓存保持您最常用的对象在内存中,当系统出来的内存SoftReference的的使用使得GarbageCollector释放这些资源。

That kind of cache keeps your most used objects in memory, and when the systems comes out of memory the usage of SoftReference allow the GarbageCollector to free these resources.

下面是我目前正在使用我的应用程序EmailAlbum实现:
<一href=\"http://$c$c.google.com/p/emailalbum/source/browse/EmailAlbumAndroid/tags/REL-2_9_3/src/com/kg/emailalbum/mobile/util/NewLRUCache.java\" rel=\"nofollow\">http://$c$c.google.com/p/emailalbum/source/browse/EmailAlbumAndroid/tags/REL-2_9_3/src/com/kg/emailalbum/mobile/util/NewLRUCache.java

Here is an implementation I am currently using in my app EmailAlbum: http://code.google.com/p/emailalbum/source/browse/EmailAlbumAndroid/tags/REL-2_9_3/src/com/kg/emailalbum/mobile/util/NewLRUCache.java

这是从这关于Java缓存实现法国后,我刚刚改名的一些方法,以允许从哈希映射,而无需修改方法调用切换到该高速缓存。

This was taken from this French post about java cache implementations, I just renamed a few methods to allow switching from Hashmap to this cache without having to modify method calls.

那么,你肯定会想避免从每个画面beeing从SD卡读取时间(或网络)减慢UI。这时候,你将不得不寻求的AsyncTask 允许以LOA图片在后台线程同时让用户与UI交互(如滚动的beeing的图片加载速度更快时)。

Then, you will certainly want to avoid the UI from slowing down each time a picture is beeing read from SDCard (or the web). That's when you will have to look to AsyncTask which allows to loa pictures in a background Thread while letting the user interact with the UI (like scroll faster even when pictures are beeing loaded).

这篇关于Android的ArrayAdapter和放大器;缓慢的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆