Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of Target in Picasso on Adapter

Im having big troubles using a Target inside an adapter. Im confused about the documentation on the 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

like image 743
Rodrigo Amaro Reveco Avatar asked Nov 25 '13 17:11

Rodrigo Amaro Reveco


People also ask

Can I use normal Picasso options for target objects?

You can still use all normal Picasso options like resize, fit, etc. Note: The Target object must be stored as a member field or method and cannot be an anonymous class otherwise this won't work as expected. The reason is that Picasso accepts this parameter as a weak memory reference.

Why should I use Picasso in Android?

The solution to all these challenges is to use Picasso in Android. Picasso is an image library for Android. It is created and maintained by SQUARE. Picasso hides all the complexity as it will deal with back on threading, loading images from the internet and even caching. Picasso makes memory efficient to resize and transform images.

How does Picasso load images from the network?

It is important to note that when you load an image, Picasso will always try memory cache first and if it can’t find the image in memory cache, it will try to find it in the disk cache . And if the image is not available in disk cache it will have to load it from the network. Picasso loads the image in two ways synchronously and Asynchronously.

Does Picasso store all images in memory?

But if you are loading a lot of images Picasso will keep the most recent one in-memory cache and tries to keep all of them in the disk cache. It is important to note that when you load an image, Picasso will always try memory cache first and if it can’t find the image in memory cache, it will try to find it in the disk cache .


1 Answers

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:

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.

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).

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.

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.

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.

Some info on how to implement equals and hashCode: What issues should be considered when overriding equals and hashCode in Java?

like image 184
frozenkoi Avatar answered Oct 23 '22 20:10

frozenkoi