airbnb epoxy - how to insert new items

3.5k Views Asked by At

I am having an issue in understanding how epoxy works in Android. There is also a tutorial here

imagine i have an adapter as follows:

    public class PhotoAdapter extends EpoxyAdapter {
  private final LoaderModel loaderModel = new LoaderModel();

  public PhotoAdapter() {
    models.add(new HeaderModel("My Photos"));
    models.add(loaderModel);
    notifyItemRangeInserted(0, 2);
  }

  public void addPhotos(Collection<Photo> photos) {
    for (Photo photo : photos) {
      int loaderPosition = models.size() - 1;
      models.add(loaderPosition, photo);
      notifyItemInserted(loaderPosition);
    }
  }
}

This is as per the example here

How would i insert new items about "photos your friends like". So i'd like a new header to say "photos your friends like" and then i need to actually add the photos which i have stored in a model already.

would i just do the following to make it work:

public void addFriendsPhotos(Collection<Photo> photosOfFriends) {

models.add(new HeaderModel("photos your friends like"));
 for (Photo photo : photosOfFriends) {
      int loaderPosition = models.size() - 1;
      models.add(loaderPosition, photo);
      notifyItemInserted(loaderPosition);
    }
  }

I am a little confused about the "models" class. I thought for example there would be a PhotosModel class and a PhotoFriendsModel class etc. instead from the example i see a HeaderModel and LoaderModel. i thought there should be a model for every item row type. Can someone explain.

UPDATE:

let us say i have more information and its not photos. Lets say i have information about addresses where the photos were taken. so now after all the photos are displayed right below the photos i want to display a huge list of addresses where EACH photo was taken. Tell me how i would add the addresses list ?

2

There are 2 best solutions below

0
On
public class PhotoAdapter extends EpoxyAdapter {
  private final LoaderModel loaderModel = new LoaderModel();

  public PhotoAdapter() {
    models.add(new HeaderModel("My Photos"));
    models.add(loaderModel);
    notifyItemRangeInserted(0, 2);
  }

  public void addPhotos(Collection<Photo> photos) {
    for (Photo photo : photos) {
      int loaderPosition = models.size() - 1;
      models.add(loaderPosition, photo);
      notifyItemInserted(loaderPosition);
    }
  }
}

You could just use models.add(loaderPosition, photo); to insert a new model.

See http://airbnb.io/projects/epoxy/

2
On

yes your right EpoxyModel is use for every view type used in your adapter

public void addFriendsPhotos(Collection<Photo> photosOfFriends) 
{

    addModel(new HeaderModel("photos your friends like"));

    for (Photo photo : photosOfFriends) 
    {
        addModel(new PhotModel(photo));
    }

 }