Use of Target in Picasso on Adapter

后端 未结 2 1356
长发绾君心
长发绾君心 2021-01-02 10:12

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

Objects implementing this class must<

相关标签:
2条回答
  • 2021-01-02 10:42

    It seems your equals method is broken. You are comparing an imageview to a custom target. This might fix it:

    public boolean equals(Object o) {
        if(o instanceof CustomTarget) {
            return ((CustomTarget) o).imageView.equals(this.imageView);
        }
        return super.equals(o);
    }
    
    0 讨论(0)
  • 2021-01-02 10:47

    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?

    0 讨论(0)
提交回复
热议问题