ListView scroll lagging when images is shown?

北慕城南 提交于 2019-12-04 21:43:49

yes, resolving the lagging by using memory cache. You can read the origin post from here.

I've implement my EntryArrayAdapter, which uses memory caching to prevent slow and lag when scrolling a list view.

public class EntryArrayAdapter extends ArrayAdapter<JSONObject> {
private final Context context;
private final ArrayList<JSONObject> values; 
private final LruCache<String, Bitmap> cache;

static class ViewHolder {
        public TextView title;
        public TextView city;
        public TextView outlet;
        public TextView service;
        public ImageView photo;
}

public EntryArrayAdapter(Context context, ArrayList<JSONObject> values) {
    super(context, R.layout.layout_list_item);
    this.context = context;
    this.values = values;

     final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

        // Use 1/8th of the available memory for this memory cache.
        final int cacheSize = maxMemory / 8;

        cache = new LruCache<String, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                // The cache size will be measured in kilobytes rather than
                // number of items.
                return bitmap.getRowBytes() / 1024;
            }
        };
}

/* (non-Javadoc)
 * @see android.widget.ArrayAdapter#getCount()
 */
@Override
public int getCount() {     
    return values != null ? values.size() : 0;
}

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

    if (rowView == null) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        rowView = inflater.inflate(R.layout.layout_list_item, parent, false);
        // set references
        holder = new ViewHolder();

        holder.title = (TextView) rowView.findViewById(R.id.titleText);
        holder.city = (TextView) rowView.findViewById(R.id.cityText);
        holder.outlet = (TextView) rowView.findViewById(R.id.outletText);
        holder.service = (TextView) rowView.findViewById(R.id.visaMasterText);      
        holder.photo = (ImageView) rowView.findViewById(R.id.itemImage);
        rowView.setTag(holder);
    }
    else {
        holder = (ViewHolder) rowView.getTag();
    }

    try {
        JSONObject obj = values.get(position);

        Bitmap bmp = getBitmapFromMemCache(obj.getString("id"));
        if (bmp == null) {
            bmp = BitmapHelper.decodeSampledBitmapFromResource(obj.getString("photo"), 96, 96);        
            addBitmapToMemoryCache(obj.getString("id"), bmp);
        }

        holder.title.setText(obj.getString("title"));
        holder.city.setText(obj.getString("city"));
        holder.outlet.setText(obj.getString("outlet"));
        holder.service.setText(obj.getString("service"));           
        holder.photo.setImageBitmap(bmp);


    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return rowView;
}

public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
    if (getBitmapFromMemCache(key) == null) {
        cache.put(key, bitmap);
    }
}

public Bitmap getBitmapFromMemCache(String key) {
    return cache.get(key);
}

I solved it by caching the image in background by creating and using the following function in ImageLoader.java

public void cacheImage(String url){
    Bitmap bmp = getBitmap(url);
    memoryCache.put(url, bmp);
}

I created one instance of ImageLoader, made a static reference to it and then called

AsyncClass.imageLoader.cacheImage(url);

Where AsyncClass is the class where I made the static reference to the ImageLoader and called it "imageLoader".

I recommend you to use Android-Universal-Image-Loader for asynchronous image loading, caching and displaying.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!