Join Albert Lo for an in-depth discussion in this video Incorporate the GridRecyclerView, part of Android Development: Modern Android UI Design Elements.
- [Narrator] Now that we have completed our view holders and our recycler view adapters, we are now ready to incorporate them into our Google Photos Fragment. So let's open up our Android Project View with Command one. And we go into our package and we go into Google Photos and open up Google Photos fragment. And we dismiss our project view. So let's scroll down to the On View Created. We want to add the recycler view and the album grid adapter here, right around line 55.
So let's go up to the beginning of the file and we'll add some private class members first. First one is Album Grid Adapter. And we'll name it Adapter. The next one is Grid Recycler View. And we will name it Grid. The final one is the Text View. This will hold our account names. We will give it the name Account Name.
Now let's scroll back down to On View Created. We're going to first initialize the account name and we cast it to Text View. We will call the Find View by ID Method of View. And we will use the ID account name. Next, we will initialize our Grid Recycler View. So we will cast to Grid Recycler View.
And also use the Find View by ID method. And we will use the View ID Photo Grid. Also, we're going to initialize our adapter. We're going to construct a new instance of Album Grid. And we will pass in the activity. We will first cast to App Compat Activity and call Get Activity. And now we can call the Set Adapter method for our Recycler View Adapter.
And pass in the adapter that we created. Now that we have initialized our views, let's also go into Recycler View and add back some missing methods. So I'm going to highlight Grid Recycler View and press Command B. Now we are in Grid Recycler View. There's a couple of changes that we want to make. First is to call IniView. And we're going to pass in context and attributes.
We will do this for each constructor. Here we actually have an attribute set, so we'll pass that in. And I'm just going to copy this. And paste it into our third constructor. Now, the next thing we want to do in our IniView, I want to add another method for the Grid Layout Manager. And we're going to call the Span Size Lookup.
We're going to construct a new Span Size Lookup. In our Get Span Size, we are going to use our adapter that we created. First let's do a validation check for our adapter. If we don't have an adapter, we're going to return zero. If we do have an adapter, we're going to get the Item View Type.
And pass in position. If our Item View Type is Header, then we're going to set the span count using the Get span Count method. We will call the manager .GetSpanCount. And finally, if we don't have a header, then we simply return one.
And also, we're going to call the set layout manager here, right below line 71, Set Layout Manager. And we pass in our manager. Now we are finished with our changes with Grid Recycler View and we're ready to head back to Google Photos Fragment.
- Creating a custom layout with XML
- Setting up OnClick handlers
- Making API calls
- Creating custom views
- Implementing ViewHolder objects
- Signing certificates
- Adding a checkbox