Android ListView重用旧图像

Android ListView重用旧图像,android,nativescript,Android,Nativescript,我使用创建了一个插件,它使用将缓存的图像加载到。 如果使用转发器,该插件可以正常工作,但每当我在滚动第7项后尝试将其与ListView一起使用时,ListView就会开始重用旧图像,即使图像源不同 原因是列表视图重用了整个片段;所以,如果你不清除它,你的img就会被重新使用,并显示旧的图像 实际上我自己也用过毕加索;这是我现在的毕加索图书馆。 因此,如果您查看下面的代码,当我设置新的.url时,我会清除现有的图像。(我在特定行上做了一个注释)——这样,图像现在显示为空白,然后毕加索从内存、磁盘或

我使用创建了一个插件,它使用将缓存的图像加载到。 如果使用转发器,该插件可以正常工作,但每当我在滚动第7项后尝试将其与ListView一起使用时,ListView就会开始重用旧图像,即使图像源不同


原因是列表视图重用了整个片段;所以,如果你不清除它,你的img就会被重新使用,并显示旧的图像

实际上我自己也用过毕加索;这是我现在的毕加索图书馆。 因此,如果您查看下面的代码,当我设置新的.url时,我会清除现有的图像。(我在特定行上做了一个注释)——这样,图像现在显示为空白,然后毕加索从内存、磁盘或远程url(在我的例子中是远程url)加载它,它将分配正确的图像

"use strict";

var Img = require('ui/image').Image;
var application = require("application");

var PT = com.squareup.picasso.Target.extend("Target",{
    _owner: null,
    _url: null,
    onBitmapLoaded: function(bitmap, from) {
        // Since the actual image / target is cached; it is possible that the
        // target will not match so we don't replace the image already seen
        if (this._url !== this._owner._url) {
            return;
        }
        this._owner.src = bitmap;
    },
    onBitmapFailed: function(ed) {
        console.log("Failed File", this._url);
    },
    onPrepareLoad: function(ed) {

    }
});

Object.defineProperty(Img.prototype, "url", {
    get: function () {
        return this._url;
    },
    set: function(src) {
        if (src == null || src === "") {
            this._url = "";
            this.src = null;
            return;
        }
        var dest = src;
        this._url = dest;
        this.src = null; // -- THIS IS THE LINE TO CLEAR THE IMAGE
        try {
            var target = new PT();
            target._owner = this;
            target._url = dest;
            var x = com.squareup.picasso.Picasso.with(application.android.context).load(dest).into(target);
        } catch (e) {
            console.log("Exception",e);
        }
    },
    enumerable: true,
    configurable: true
});

请注意,您只需要一次这个类,然后它将自身附加到组件并添加新的.url属性;这允许我在所有其他屏幕的声明性XML中使用它,当我需要毕加索时,我只需使用.url属性让毕加索接管该图像的加载。

原因是列表视图重用了整个片段;所以,如果你不清除它,你的img就会被重新使用,并显示旧的图像

实际上我自己也用过毕加索;这是我现在的毕加索图书馆。 因此,如果您查看下面的代码,当我设置新的.url时,我会清除现有的图像。(我在特定行上做了一个注释)——这样,图像现在显示为空白,然后毕加索从内存、磁盘或远程url(在我的例子中是远程url)加载它,它将分配正确的图像

"use strict";

var Img = require('ui/image').Image;
var application = require("application");

var PT = com.squareup.picasso.Target.extend("Target",{
    _owner: null,
    _url: null,
    onBitmapLoaded: function(bitmap, from) {
        // Since the actual image / target is cached; it is possible that the
        // target will not match so we don't replace the image already seen
        if (this._url !== this._owner._url) {
            return;
        }
        this._owner.src = bitmap;
    },
    onBitmapFailed: function(ed) {
        console.log("Failed File", this._url);
    },
    onPrepareLoad: function(ed) {

    }
});

Object.defineProperty(Img.prototype, "url", {
    get: function () {
        return this._url;
    },
    set: function(src) {
        if (src == null || src === "") {
            this._url = "";
            this.src = null;
            return;
        }
        var dest = src;
        this._url = dest;
        this.src = null; // -- THIS IS THE LINE TO CLEAR THE IMAGE
        try {
            var target = new PT();
            target._owner = this;
            target._url = dest;
            var x = com.squareup.picasso.Picasso.with(application.android.context).load(dest).into(target);
        } catch (e) {
            console.log("Exception",e);
        }
    },
    enumerable: true,
    configurable: true
});

请注意,您只需要一次这个类,然后它将自身附加到组件并添加新的.url属性;这允许我在所有其他屏幕的声明性XML中使用它,当我需要picasso时,我只需使用.url属性让picasso接管该图像的加载。

能否提供ListView使用的适配器类的代码?My代码,您可以提供用于ListView的适配器类的代码吗?我的代码,谢谢,但我使用了
onImageSourcePropertyChanged(data:PropertyChangeData){var image=data.object;if(!image.android){return;}image.\u setNativeImage(data.newValue?data.newValue.android:null);}
我不明白你想说什么。你是说你知道如何使用上面的方法来正确地清除图像吗?或者你在做完全不同的代码?我开始用另一种方法解决我的问题谢谢,但我使用了
onImageSourcePropertyChanged(data:PropertyChangeData){var image=data.object;if(!image.android){return;}image.\u setNativeImage(data.newValue?data.newValue.android:null);}
我不明白你想说什么。你是说你知道如何使用上面的方法来正确地清除图像吗?或者你在做完全不同的代码?我用另一种方法解决了我的问题