不知何时在ListView视图已经关闭屏幕? [英] Knowing when a View in a ListView has gone off the screen?

查看:140
本文介绍了不知何时在ListView视图已经关闭屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Google搜索这一点,但无法找到答案,所以这里去...

I have Googled this but can't find an answer, so here goes...

我有一个显示一些文本和图像一个ListView。底层的适配器回收性能方面的原因的看法(按推荐的方法),以及我加载使用AsynchTask按这里发现这里推荐图像:<一href=\"http://developer.android.com/training/displaying-bitmaps/index.html\">http://developer.android.com/training/displaying-bitmaps/index.html

I have a ListView that displays some text and an image. The underlying adapter recycles the views for performance reasons (as per the recommended approach), and I load the images using an AsynchTask as per the recommendation found here here: http://developer.android.com/training/displaying-bitmaps/index.html

一切完美,顺利,但我有一个问题。当适配器查看被回收,它仍然有旧的图像(ImageView的)的引用。如果用户滚动慢,那么AsynchTask有足够的时间来加载新的形象,显示它,于是就有了新的形象,对用户没有明显的重载。

Everything works perfectly and smoothly, but I have one issue. When the View in the adapter is recycled, it still has a reference to the old image (ImageView). If the user scrolls slowly, then the AsynchTask has enough time to load the new image and display it, so there is no visible reloading of the new image to the user.

但是,如果用户滚动速度非常快,在加载图像延迟意味着他们看到了新的形象取代了旧的图像(当使用的观点得到了其他项目已加载),其前。

However, if the user scrolls very quickly, the delay in loading the image means they see the old image (that was loaded when the View was being used by another item) before its replaced by the new image.

所以,我的问题是,如何检测时查看任何屏幕上不再可见,所以后来我可以删除的形象呢?这意味着用户可以看到最终将被装入相应的图像空的列表视图项,这将更好看。

So, my question is, how can I detect when a View is no longer visible on the screen, so I can then remove the image? This would mean the user sees an empty list view item that will eventually be loaded with the appropriate image, which would look better.

提前非常感谢。这里是列表视图适配器code。

Many thanks in advance. Here is the list view adapter code.

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View view = convertView;
    ListViewHolder viewHolder;

    // if this is not a recycled view then create a new view for the data...
    if (view == null)
    {
        view = this.inflater.inflate(R.layout.target_list_view_layout, null, true);

        viewHolder = new ListViewHolder();

        viewHolder.manufacturer = (TextView) view.findViewById(R.id.manufacturer);
        viewHolder.targetName = (TextView) view.findViewById(R.id.targetName);
        viewHolder.targetThumbnail = (ImageView) view.findViewById(R.id.targetThumbnail);

        view.setTag(viewHolder);
    } else
    {
        viewHolder = (ListViewHolder) convertView.getTag();
    }

    TargetDescriptor targetDescriptor = this.selectedTargets.get(position);

    viewHolder.manufacturer.setText(targetDescriptor.manufacturer);
    viewHolder.targetName.setText(targetDescriptor.targetName);

    // At this point I pass the image view reference to my background task to load the image
    LoadImageViewAsynchTask loadImageTask = new LoadImageViewAsynchTask(viewHolder.targetThumbnail, targetDescriptor);
    loadImageTask.execute(new Integer[]
    { 64, 64 });

    return view;
}

编辑:那些使用eBay的Andr​​oid应用程序可以看到我正在寻找有没有什么帮助作用

Those that use the eBay Android App can see the effect I am looking for if that helps.

推荐答案

简直不敢相信我是多么愚蠢一直,这是一个非常简单的一班轮解决!

Can't believe how stupid I have been, this is a really easy one liner to solve!

基本上,如果视图被重新循环,我只需要为null ImageView的位图重新使用视图前。这意味着下次加载图像之前在列表视图项时图像显示空白。我不再有旧的图像是有问题,而新的图像在后台加载。

Basically in my adapter if the view is being re-cycled, I simply need to null the ImageView bitmap before the view is re-used. That means the image in the list view item is blank before the next image is loaded. I no longer have the issue of the old image being there whilst the new image is being loaded in the background.

我也取得了在取消绑定到仍可能加载一个不再需要previous图像的任何视图当前AsynchTask适配器的变化。这是很prevalent当我滚动速度非常快,通过列表视图中,经常与两个或更多的图像加载备份,所以解决的正确图像之前的图像会改变几次。

I have also made a change in the adapter that cancels any current AsynchTask bound to the view that might still be loading a previous image that is no longer needed. This was very prevalent when I scroll very fast through the list view, with often two or more image loads backing up, so the image would change several times before settling on the correct image.

下面是新的code,以改变注释,以便你可以看到发生了什么:

Here is the new code, with the changes commented so you can see what is happening:

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

    // if this is not a recycled view then create a new view for the data...
    if (convertView == null)
    {
        convertView = this.inflater.inflate(R.layout.target_list_view_layout, null, true);

        viewHolder = new ListViewHolder();

        viewHolder.manufacturer = (TextView) convertView.findViewById(R.id.manufacturer);
        viewHolder.targetName = (TextView) convertView.findViewById(R.id.targetName);
        viewHolder.targetThumbnail = (ImageView) convertView.findViewById(R.id.targetThumbnail);

        convertView.setTag(viewHolder);
    } else
    {
        viewHolder = (ListViewHolder) convertView.getTag();

        // Cancel the previous attempt to load an image as this is going to be superceded by the next image
        viewHolder.loadImageViewAsynchTask.cancel(true);

        // Clear down the old image so when this view is displayed, the user does not see the old image before the
        // new image has a chance to load in the background
        viewHolder.targetThumbnail.setImageBitmap(null);
    }

    TargetDescriptor targetDescriptor = this.selectedTargets.get(position);

    viewHolder.manufacturer.setText(targetDescriptor.manufacturer);
    viewHolder.targetName.setText(targetDescriptor.targetName);

    LoadImageViewAsynchTask loadImageViewAsynchTask = new LoadImageViewAsynchTask(viewHolder.targetThumbnail);
    loadImageViewAsynchTask.setTargetDescriptor(targetDescriptor);
    loadImageViewAsynchTask.execute(new Integer[]
    { 64, 64 });

    // Keep a reference to the task so we can cancel it if the view is recycled next time round to prevent
    // un-neccessary image loads that are out of date
    viewHolder.loadImageViewAsynchTask = loadImageViewAsynchTask;

    return convertView;
}

这篇关于不知何时在ListView视图已经关闭屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆