Create GridPhotoItem POJO that supports multiple view types.
- [Instructor] Next, let's define the model. In my opinion, this is the most interesting part of the RecyclerView. Every RecyclerView adapter has a model. Like all models, the RecyclerView model has no dependencies on the Android framework. So it will be easy to implement and test. Our model will have two parts to it. The first one is the GooglePhotosItem, and this will contain the backing data for our RecyclerView adapter.
The other one will be the GooglePhotosItemManager, and this will provide methods to access and manage our Google Photos items. So let's go ahead and create our GooglePhotosItem. I'm going to press Command + 1 to open the Android project view, and we're going to go into app, go into our package, and under models, we are going to create a new model, and we're going to call it GooglePhotosItem.
Our GooglePhotosItem is purely a POJO, plain old Java object. Our GooglePhotosItem will contain an enum, which will define the itemType. So let's go ahead and create our enum here, and we're going to call it ItemType. Our first ItemType will be ALBUM_HEADER, and we're going to pass in a string for the constructor calling it ALBUM_HEADER.
We have a second parameter. I'm going to define it as type. Our next enum will be PHOTO_HEADER. And we're passing in a type 2. These type will be used by the RecyclerViewAdapter on view type. The next enum will be ALBUM, and I'm going to pass in a view type of three.
The last one will be PHOTO, and the view type is four. We're going to create our constructor ItemType. It will take the two parameters. The first parameter will be a name of type String. The second parameter will be type. Let's define our private class members. The first one will be String name, and the second one will be type.
And we will initialize our private class members and our constructor. So the next thing we want to do in our enum is add a method we're going to call getViewType, which will return our type.
We're also going to add a method called getName, which will return our name. And we're going to add a public static method, which will return our itemType enum from an integer view type. Public static method returns ItemType enum, and we're going to call this method fromViewType, passing in our type.
We're going to iterate through all of our enums. So we're going to call the values method for our enum. So, for each enum that we're iterating through, we're going to look for a match with our type. So we'll retrieve our getViewType and check if it is equal to the passed in type.
And if it is, we're going to return our itemType enum. If we do not find a match, we're going to throw an exception. Okay. So the next thing for our GooglePhotosItem is we're going to add some class members here. We have a few class members. We're going to add our album item from the Picasa client call, and we're going to add our photo entries.
So let's first create a class member for AlbumEntry, and we'll create a class member for PhotoEntry. We're also going to add a couple of class members. One of them will be timestamp. We're also going to store the view type in here and a selection state. So, first is the Timestamp of type String, and then next is the viewType, and the last one is the selection state.
Now let's go down to the bottom of the class, and we're going to add the getters and setters for the class members that we just created. So I'm going to go right-mouse, generate, and I'm going to select Getter and Setter. I'm going to create getters and setters for Timestamp, the album, photo items, timestamp, viewType, and isSelected.
The last thing we want to do is add our constructors. We have three different types of constructors that we want to create. The first one we'll pass in is timestamp, and we're going to initialize our viewType here to header. And we're going to initialize our timestamp as well.
Next constructor we want to add will take in a constructor parameter of PhotoEntry. And again, we're going to initialize our viewType to PHOTO and initialize our photoItem. The last constructor that we want to add will be a constructor that will take in an album entry.
And it'll be similar to what we did earlier with the photos. Our viewType will be album instead this time. And we're going to initialize our albumItem. Okay. Now we are finished with creating our GooglePhotosItem.
- Creating a custom layout with XML
- Setting up OnClick handlers
- Making API calls
- Creating custom views
- Implementing ViewHolder objects
- Signing certificates
- Adding a checkbox