在毕加索使用目标的适配器 [英] Use of Target in Picasso on Adapter

查看:121
本文介绍了在毕加索使用目标的适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林具有使用适配器内的目标很大的麻烦。林困惑的code

Im having big troubles using a Target inside an adapter. Im confused about the documentation on the code

对象实现这个类的必须有一个工作的实施     {@link #equals(对象)}和{@link#散code()}妥善存放在内部。这个实例     接口也将被比较,以确定是否视图回收正在发生。建议     你当适配器使用,以保证直接添加此接口的自定义视图类型     正确的回收行为。

Objects implementing this class must have a working implementation of {@link #equals(Object)} and {@link #hashCode()} for proper storage internally. Instances of this interface will also be compared to determine if view recycling is occurring. It is recommended that you add this interface directly on to a custom view type when using in an adapter to ensure correct recycling behavior.

我试着去使用的目标是这样的:

Im trying to use the Target in this way:

class CustomTarget implements Target {
    private ImageView imageView;

    public CustomTarget(ImageView imageView) {
        this.imageView = imageView;
    }

    @Override
    public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
        imageView.setImageDrawable(new RoundedAvatarDrawable(bitmap));
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        imageView.setImageDrawable(errorDrawable);
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
        imageView.setImageDrawable(placeHolderDrawable);
    }

    @Override
    public boolean equals(Object o) {
        return imageView.equals(o);
    }

    @Override
    public int hashCode() {
        return imageView.hashCode();
    }
}

 @Override
public View getView(int position, View v, ViewGroup parent) {
....
    RoundedAvatarDrawable r = new RoundedAvatarDrawable(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_avatar_seahorse));
    ImageCacheController.with(mContext).getPicasso().load(member.getPicture_url()).resize(100, 100).centerCrop().placeholder(r).error(r).into(new CustomTarget(viewHolder.ivAvatar));
....
}

这不工作,并随机彼此之间的图像变化

It's doesn't work and the images change between each others randomly

推荐答案

您不会显示你的整个 getView 的功能,所以不知道如何使用的ViewHandler,这里的我拿上这是怎么回事:

You don't show your whole getView function, so without knowing how you use the viewHandler, here's my take on what's going on:

您的问题是,您要创建一个新的 CustomTarget 每次 getView 被调用。你要对具有目标对象的点。让我解释。

Your problem is that you're creating a new CustomTarget every time getView gets called. You are going against the point of having a Target object. Let me elaborate.

在一个新的下载请求时,previous请求到同一目标得到停止或不会导致调用目标的回调。 (因此,如果目标被以列表重新用于不同的行它没有得到两行图像)。

When a new download request is made, previous requests to the same target get stopped or don't result in a call to the Target's callbacks. (so if the Target gets reused for a different row in a list it doesn't get both rows' images).

您正在使用一个新的对象为每个请求,有力地暗示毕加索每个请求的是一个不同的行可以这么说。该医生说的此接口的实例也将进行比较,以确定是否认为回收发生的,如此以来,每个请求有一个新创建 CustomTarget 对象,没有两个请求将具有相同的对象和一个行循环将不会被检测到。

You are using a new object for each request, effectively hinting Picasso that each request is for a different row so to speak. The doc says "Instances of this interface will also be compared to determine if view recycling is occurring", so since each request has a newly created CustomTarget object, no two requests will have the same object and a row recycle won't be detected.

您还使用viewHolder。在这种情况下,我觉得viewHolder应延长目标接口(如果你只有每行1图像)。这种方式每次您申请一个下载,你可以使用相同的对象,而不是创建一个新的。

You're also using viewHolder. In this case I think the viewHolder should be extending the Target interface (if you only have 1 image per row). This way everytime you request a download you can use the same object and not create a new one.

你还委托你的 CustomTarget 的ImageView 的实施执行。确保的ImageView 等于散code 功能fullfill要求毕加索要求。

You're also delegating the implementation of your CustomTarget to the ImageView's implementation. Make sure that ImageView's equals and hashCode functions fullfill the requirements Picasso asks for.

这是如何实现的一些信息等于散code Overriding平等和哈希code在Java中

Some info on how to implement equals and hashCode: Overriding equals and hashCode in Java

这篇关于在毕加索使用目标的适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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