How to avoid image flickering in a listview

前端 未结 5 1101
伪装坚强ぢ
伪装坚强ぢ 2021-01-14 02:09

I have a listivew that display a bunch of images. am using Universal Image Loader to load this images from files to imageviews.

This images have dif

5条回答
  •  隐瞒了意图╮
    2021-01-14 02:20

    The reason for this flicker is that, in listview list items are reused. When re-used, the imageviews in the list item retains the old image reference which is displayed first. Later on once new image is downloaded, it starts to show. this causes the flickering behavior. To avoid this flickering issue, always clear the old image reference from the imageview when it is getting reused.

    In your case, add holder.image.setImageBitmap(null); after holder = (ViewHolder) convertView.getTag();

    So, your getView() method will look like:

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    
        ...
    
        if (convertView == null) {
            LayoutInflater inflater = getLayoutInflater();
            convertView = inflater.inflate(viewResourceId, null);
    
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            holder.image.setImageBitmap(null)
        }
    
        ...
    
        return convertView;
    }
    

提交回复
热议问题