Join Albert Lo for an in-depth discussion in this video Implement AlbumGridAdapter, part of Android Development: Modern Android UI Design Elements.
- [Commentator] Now we are ready to create our album grid adapter. Before we do that, you may want to press play here to build your project to make sure that there are no errors. Album grid adapter is our actual recycler view adapter. And it's going to extend base grid adapter. So let's go ahead and create that, I'm going to press Command+1 to open our project view. We are going to select UI.Googlephotospackage. Right mouse on it to create our new class, and we will call it album grid adapter.
As previously mentioned, album grid adapter will extend base grid adapter. Album grid adapter will also maintain a reference to an activity so let's add that class member here. It's going to be a private class member of type app compat activity. Let's add our constructor here. And the constructor will take in one parameter, app compat activity.
And in our constructor we're going to initialize our activity. The next method that we want to implement for this class will be on create view holder. Again, on create view holder is a standard framework method provided by your typical recycler view. In our case, on create view holder will return base view holder. And it will take to parameters.
I'm going to press Command+1 to close our project view. And we'll now add our two parameters, the first one is view group parent. And the other one is view type. In our design, we will create all of our view holders in on create view holder. The type of view holder that we want to create will come from Google photos item, so let's add our Google photos item item type.
And we will get our item type by calling the from view type method for Google photos item. And we pass in our view type to this method. If we cannot find our view type, then we're going to declare an exception. We'll add an error string here for no view holder for this type.
Now we're ready to create our individual view holders, we are going to switch on our type. First case we're going to add is for album. And we'll simply return a new album view holder. This will take in two parameters, the first is an activity and the other is the view group.
The next case we will add is for photos. And it will be similar to album, we're going to return a new photos view holder. We'll actually call this Google photos view holder. It will take in three parameters, the first one is activity, the second one is a reference to album grid adapter.
And the last one is the view group. Our last two cases will be for the headers, so we have one for album header, and we have one for photos header. They're very similar to each other so I can simply just return a header view holder, which will handle both cases.
Header view holder will take two parameters, one is activity the other is parent. This should cover all of the view types that was defined in Google photos item. Just in case though, if we encounter some strange error let's also add in a default case. And here we're going to throw an exception. And we'll add our error string unknown view type.
There are two other methods that we want to add, and the first one is set album list, it's going to be a public method. And it will take in a collection of album entries as the parameter. In this method we're going to delegate to Google photos manager to set the album list. And we're also going to call our adapter to notify that the data has changed.
The other method will be set photos list. And it's very similar to set albums list. It will take in a collection of photo entries. And again, we're going to delegate to Google photos manager to set the photos list. And just like before, we're going to tell our adapter that the data has changed.
Okay, now we have finished adding all our code to album grid adapter. Let's scroll back up, in the next section we're going to take a look at adding these viewholders.
- Creating a custom layout with XML
- Setting up OnClick handlers
- Making API calls
- Creating custom views
- Implementing ViewHolder objects
- Signing certificates
- Adding a checkbox