【问题标题】:ListView scrolling using UniversalImageDownloader not smooth使用 UniversalImageDownloader 的 ListView 滚动不流畅
【发布时间】:2013-08-13 19:09:09
【问题描述】:

我正在使用包含图像的 ListView。这些图像是从适配器内部的 Internet 加载的。因此我使用 UniversalImageDownloader

不幸的是,当我向下滚动必须下载新内容的位置时,ListView 的滚动会“滞后”一小段时间。

我确实期望像 ListView 这样的行为会非常流畅地滚动,但加载图像当然需要更多时间——这不会影响滚动的流畅度。

此外,当我向上滚动时,也会出现滞后。好像图片没有正确缓存。

可能是我的 ImageLoader 选项设置错误?

我的适配器有问题吗?

ListView 包含大约 20-30 张图片,大小为 640x320(约 150kb)

您可以在下面看到我的适配器以及 ImageLoader。 (Downloader 类只是 UniversalImageDownloader 的包装类)

public class Downloader {

    /**
     * initializes the imagedownloader with a specific configuration
     * I CALL THIS METHOD RIGHT AFTER APP STARTUP
     * @param c
     */
    public static void initialize(Context c) {

         // Create global configuration and initialize ImageLoader with this configuration

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(c)
        .threadPoolSize(20) 
        .threadPriority(Thread.NORM_PRIORITY) // default
        .tasksProcessingOrder(QueueProcessingType.FIFO) // default
        .memoryCacheSize(20 * 1024 * 1024)
        .memoryCacheSizePercentage(15) // default
        .discCacheSize(20 * 1024 * 1024)
        .discCacheFileCount(100)
        .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
        .imageDecoder(new BaseImageDecoder()) // default
        .build();

        ImageLoader.getInstance().init(config);
    }

    /**
     * gets the display options that are needed when displaying an image
     * @return
     */
    public static DisplayImageOptions getDisplayOptions() {

         DisplayImageOptions options = new DisplayImageOptions.Builder()
        .showImageForEmptyUri(R.drawable.error)
        .showImageOnFail(R.drawable.error)
        .resetViewBeforeLoading(false)  // default
        .cacheInMemory(true) // default
        .cacheOnDisc(true) // default
        .build();

        return options;
    }

    public static ImageLoader getInstance() {
        return ImageLoader.getInstance();
    }
}

还有适配器:

public class EventListAdapter extends ArrayAdapter<Event> {

    private List<Event> mList;
    private DisplayImageOptions options;

    public EventListAdapter(Context context, int list_item_resource, List<Event> objects) {
        super(context, list_item_resource, objects);
        mList = objects;

        options = Downloader.getDisplayOptions();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Event event = mList.get(position);

        // A ViewHolder keeps references to children views to avoid unneccessary calls to findViewById() on each row.
        ViewHolder holder = null;

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.normalevent_list_item, parent, false);

            holder = new ViewHolder();;

            holder.eventimage = (ImageView) convertView.findViewById(R.id.ivEventImage);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();

        }

        if (event != null) {

            holder.eventimage.setImageResource(R.drawable.loading);
            // Load image, decode it to Bitmap and display Bitmap in ImageView
            Downloader.getInstance().displayImage(event.getImageOneURL(), holder.eventimage, options);
        }

        return convertView;
    }

    private static class ViewHolder {   

        ImageView eventimage;
    } 
}

【问题讨论】:

    标签: android android-listview lag smooth-scrolling universal-image-loader


    【解决方案1】:

    我在下载图片时遇到了同样的问题。我通过在 DisplayImageOptions 中设置 delayBeforeLoading(1000) 解决了这个问题。当用户停止投掷时需要开始下载。

    所以尝试用这个替换你的 getDisplayOptions 方法

     public static DisplayImageOptions getDisplayOptions() {
    
         DisplayImageOptions options = new DisplayImageOptions.Builder()
        .showImageForEmptyUri(R.drawable.error)
        .showImageOnFail(R.drawable.error)
        .delayBeforeLoading(1000) 
        .resetViewBeforeLoading(false)  // default
        .cacheInMemory(true) // default
        .cacheOnDisc(true) // default
        .build();
    
        return options;
    }
    

    文档还建议使用此代码来避免网格/列表视图滚动滞后

    boolean pauseOnScroll = false; // or true
    boolean pauseOnFling = true; // or false
    PauseOnScrollListener listener = new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling);
    listView.setOnScrollListener(listener);
    

    您可以阅读它here(“有用信息”列表的最后一项)

    所以你可以使用其中一种方法

    【讨论】:

    • 滞后消失了,非常感谢! :) PS:我将其设置为 100 毫秒
    • PauseOnScrollListener 构造函数上的 imageLoader 是什么?
    【解决方案2】:

    @Sergey Pekar:你救了我的命! :) 我总是将 UIL 用于 ListView 的少量图像,但是随着数量的增加,我总是在图像的第一次加载(滚动时)时出现延迟。 所以我也尝试了延迟,它对其他 UIL 配置和选项有所帮助,但我并不高兴。 然后我尝试了不同的图像加载库(毕加索、AQuery、Volley、UrlImageViewHelper、自定义代码)。它们都比 UIL 工作得更快(在第一次加载时),但它们对我来说没有足够的选择。 因此,我在过去 2 天寻找有关此滞后问题的解决方案,感谢上帝,我终于在这里找到了您的帖子! 解决方案是 PauseOnScrollListener!

    还有什么(除了 PauseOnScrollListener)通过扩展 ImageView 来停止 requestLayout 来稍微提高滚动平滑度,如下所示:

    public class CustomImageView extends ImageView
    {
        public CustomImageView (Context context, AttributeSet attributeset, int int_style)
        {
            super(context, attributeset, int_style);
        }
    
        public CustomImageView (Context context, AttributeSet attributeset)
        {
            super(context, attributeset);
        }
    
        public CustomImageView (Context context)
        {
            super(context);
        }
    
        @Override
        public void requestLayout() 
        {
            // Do nothing here
        }
    }
    

    有关更多信息,请参阅此帖子:
    ListView very slow when scrolling (using ViewHolder/recycling)

    【讨论】:

    • 你救了我的命!已经一个星期了。我尝试了所有可用的库(超过 10 个)。但是我的列表视图在投掷时非常缓慢。我也尝试过使用 UIL 的 PauseOnScrollListener。但是您的 CustomImageView 成功了。现在我的列表和 Wind 一样快 :)
    • 所以我使用了相同的组合。带有此 CustomImageView 的 UIL
    • 很高兴听到 Seshu :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多