Join Albert Lo for an in-depth discussion in this video Define the BaseGridAdapter, part of Android Development: Modern Android UI Design Elements.
- [Commentator] Now we are ready to create our recycler view adapter. I've already named it base grid adapter here. So let's also fix the build warning at the same time. I'm moving our cursor over to base grid adapter and we press Alt+Return. We're going to create a new class. And we're going to place our class into Google photos, so let's delete viewholders. Press okay.
Now we have our new class, base grid adapter. Base grid adapter will extend recyclerview.adapter. And it will be of type base view holder. Base grid adapter will maintain a reference to Google photos manager, so let's create that class member here. It's going to be a protected member. And we will call it Google photos manager.
Let's create a constructor here, it will be a public constructor and we'll just call it base grid adapter. We will not need any input parameters. And in our constructor we're going to initialize Google photos manager. Now we're going to implement a couple more methods in this class. The first method is on bind view holder. On bind view holder is a standard framework method provided by your typical recycler view.
It's public. Call it on bind view holder. We're going to pass in base view holder as our viewholder. And also the position. On bind view holder will delegate the binding functionality to the view holder itself. In our design, all viewholders will know how to bind itself.
So we're going to defer that responsibility to the viewholders, let's call holder and the abstract method set data, and we're going to pass in a reference to base grid adapter itself, which is this. We're going to pass in a reference to Google photos item, and we will do that by making a new method called get item. Which takes in position as a parameter.
And finally we're going to pass in position also. Now let's define this get item method. It will be public, and it returns a Google photos item. It will take position as an input parameter. And we can get the item just by calling Google photos manager, there's a method called get item.
Which takes in the position. Another method that we want to add is get item view type. It's also a standard framework method provided by recycler views, so we override, it's a public method. And it takes int position. Get item view type is a key method, not only for this class, but also for the whole recycler view in the context of multiple view type support.
When we scroll a recycler view, it will eventually call this method to obtain the view type to be used to display the view, so what we are actually defining here are all of the different view types. In a typical recycler view, we may not know ahead of time what the view type for each recycler view item at that specified position will be. However we can create rules and rely on these rules to define what view type will go at what position. In our case, Google photos item defines and is responsible for the view type, and we will return it here.
So let's call get item. Passing in our position, and now we have our item, we'll just return the view type. One last method that we need to implement is get item count. It's another framework method. And we will just delegate this to Google photos manager to return that item count.
Android studio is flagging an error with our item.getviewtype method call, so let's go ahead and take a look at how we can fix this, I'm going to go to Google photos item and press Command+B. Now with Google photos item, we used Android studio to auto generate getters and setter methods for us, and one of them is get view type. We want get view type to return an integer instead of item type, so let's correct that by returning int.
And we're going to call get viewtype, there's a method called getviewtype, which returns int. That should fix this problem, and now we have fixed the error, let's scroll to the top to check that everything is okay. And we see here that Android studio is also flagging another error, so what we need to do is make this an abstract based class and I can do that just by adding abstract keyword here. So now we've checked that the file is okay, no more errors, and now we're good.
- Creating a custom layout with XML
- Setting up OnClick handlers
- Making API calls
- Creating custom views
- Implementing ViewHolder objects
- Signing certificates
- Adding a checkbox