Listview shows wrong images

后端 未结 1 1057
粉色の甜心
粉色の甜心 2020-12-10 19:54

I have a ListView with an ArrayAdapter holding rows with a Image and a String. This worked fine until I decided that the loading of the images was to slow so I could not loa

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-10 20:35

    The problem comes from your convertView: it's the same single instance that is used throughout the list, so when your asynchronous loading is complete, the image is changed when the listview is trying to paint a different item using the same convertView (or in that case, its child imageView).

    painting position 1, uses placeholder, starts loading image 1 asynchronously
    painting position 2, uses placeholder, starts loading image 2 asynchronously
    image 1 loading is complete, calling setImageBitmap on imageView
    painting position 3, uses image 1, starts loading image 3 asynchronously
    etc.
    

    What you can do though is keep a cache of Bitmaps in the listadapter. Something like this:

    private Bitmap[] bitmapList;
    private Bitmap bitmapPlaceholder;  
    
    private void initBitmapListWithPlaceholders(){ 
    // call this whenever the list size changes
    // you can also use a list or a map or whatever so you 
    // don't need to drop all the previously loaded bitmap whenever 
    // the list contents are modified
        int count = getListCount();
        bitmapList = new Bitmap[count];
        for(int i=0;i

    0 讨论(0)
提交回复
热议问题