Search code examples
androidpicassoandroid-ionrobobinding

How to use Robobinding with async image loading?


How can i use Robobinding's AdapterView together with dynamic image loader , like ion or picasso ?

I'm getting a list of locations from a REST-Service, where one of the properties is an url to an image which i want to show in my view.

This is what i have :

@org.robobinding.annotation.PresentationModel
public class LocationAdapterPresentationModel implements HasPresentationModelChangeSupport {
private final PresentationModelChangeSupport changeSupport;
private final Activity activity;
List<Location> locations;

public LocationAdapterPresentationModel(Activity activity, List<Location> locs){
    locations = locs;
    this.changeSupport = new PresentationModelChangeSupport(this);
    this.activity = activity;
}

@ItemPresentationModel(LocationPresentationModel.class)
public List<Location> getLocations(){
    return new ArrayList<Location>(locations);
}

public void refreshLocations() {
    changeSupport.firePropertyChange("locations");
}

public void setLocations(List<Location> locs){
    this.locations = locs;
}

@Override
public PresentationModelChangeSupport getPresentationModelChangeSupport() {
    return changeSupport;
}

}

@org.robobinding.annotation.PresentationModel
public class LocationPresentationModel implements ItemPresentationModel<Location> {

...
my getter / setter 
...
}

But where to put the dynamic loader. It would be easy when it's just one item, but how to get it to work with each entry in the list of locations?


Solution

  • The ItemPresentationModel comes with an updateData method where i put in all the needed stuff. That solves my problem.

      @Override
    public void updateData(Location location, ItemContext itemContext) { 
          ViewGroup itemView = (ViewGroup) itemContext.getItemView();
        ImageView image1 = (ImageView) itemView.findViewById(R.id.image1);
        ImageView image2 = (ImageView) itemView.findViewById(R.id.image2);
    
        Ion.with(image1)
                .placeholder(R.drawable.loading)
                .error(R.drawable.emptypicture)
                .animateLoad(R.anim.loading_circle)
                .load(location.getPreviewImg1());
    }