开发者

Android listview image loading problem

I have an application that contains a couple of listviews. The listviews contains items that consist of imageviews and textviews.

All images are thumbnail sized on a server and the pattern used for loading them is like this:

  • Instantiate a DrawableMana开发者_StackOverflow中文版ger

  • in the getView() method I do the following:

    1. Pass the thumb uri and the ImageView instance to the DrawableManagers getImageAsync method

    2. The method will first look on sd card if the image exists if so load it from SD card and save a softreference + update imageview drawable

    3. If not exists on sd. Fetch from HTTP and save on SD (if there is enough space) put as softreference and update imageview drawable.

When the images exists on sd card everything works fine. But first time (or when using the app without sd card) the images seems to be populated into the wrong listviews rows when scrolling. When i stop scroll the problem fixes it self after a couple of seconds.

Its almost like if the ImageView references are pooled or something.

Any ideas?

I also include the getView method:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder vh;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.informationrow, null);
        vh = new ViewHolder();
        vh.imageView = (ImageView) convertView.findViewById(R.id.rowInformationIcon);
        vh.textView = (TextView) convertView.findViewById(R.id.rowInformationTitleLine);
        convertView.setTag(vh);
    }
    else {
         vh = (ViewHolder) convertView.getTag();
    }

    CustomCategory cc = items.get(position);
    if (cc != null) {
        vh.textView.setText(cc.get_name());
        if (cc.getMediaUrl() != null) {
            _drawMgr.fetchDrawableOnThread(cc.getMediaUrl(), vh.imageView);
             vh.imageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.imageframe));
        }
        else {
             vh.imageView.setImageDrawable(getResources().getDrawable(R.drawable.trans4040));
            vh.imageView.setBackgroundDrawable(null);
        }
    }

    return convertView;
}


This is the View recycling used by ListView...

The convertView parameter passed to your getView() can refer to an existing item that has scrolled off the displayed part of the list, and can be reused to show an item that is now appearing.

So, yes, the same ImageView will be reused in multiple downloads in the code you posted. In your getView() you should check to see if a download is already pending and cancel it if it's no longer needed (or let it complete to a FIFO image cache somewhere, but not touch the ImageView which is now needed for a more recently-started download).

(An alternative, lazy developers implementation that assumes infinite memory would be to ignore the convertView parameter and instantiate fresh informationrow views on every call to it. Don't do that. :) ).


Use this Library for loading images in ListView. https://github.com/nostra13/Android-Universal-Image-Loader

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜