Create a custom RecyclerView specifically to work with GridViews.
- [Instructor] The primary purpose of our GridRecyclerView is to support setting configurable width grid items. This is similar to grid views column width parameter. So, to do that the overall plan is to add a columnWidth attribute in the layout XML and then read it into our custom GridRecyclerView. So, let's create our custom GridRecyclerView. I'm going to press command one to bring up the Android project view and go into Java, go into our package and under the UI we'll create a new package called views.
Under the views package we will create our GridRecyclerView. GridRecyclerView is a custom extension of the RecyclerView class, so let's enter that here and now we press control O to insert our standard constructors and I'm going to press command one to dismiss our project view.
Our custom GridRecyclerView will have a couple of methods. The first one is an initView, so let's create a private method called intiView. We will pass in our context and our AttributeSet. In our initView method we are going to add a columnCount, so let's create an integer for columnCount and we're going to make a couple of initialization calls.
The first one is setHasFixedSize true. Set it to true and the next one is to set our ItemAnimator to a default and if we wanted to add dividers and lines we would also make a call here to addItemDecorator. But we don't really need that right now, so I just wanted to highlight that.
For our columnCount we're going to create a new method called getColumnCount and we're going to pass in context and our AttributeSet. Let's define our getColumnCount. We'll create a new method, a private method and which returns an integer and we're going to pass in context and AttributeSet again.
We'll define a couple of local variables. The first one is columnCount. The second is the screenWidth. And we will call the resources to get the physical pixel width and I'm going to add in an arbitrary number here because you want a screen width that is actually less than the physical screen width of the device. I'm going to create an attribute array so that we can pass it into a framework call later on.
Our attribute array will contain one element which is the columnWidth. Now we're going to make a framework call to obtain StyledAttributes and we'll pass in our attributes array and the AttributeSet. Now we'll take our array and we will call getDimensionPixelSize.
We are going to pass in an index of zero which means we'll request the first element of that array and we'll pass in a default value of minus one. Let's go to the beginning of our file. I'm going to copy this columnWidth and define it as a class member up here. It's a private class member. Type int with a default value of negative one. Now, that we have our columnWidth, we are done with our TypedArray so let's call recycle on it.
And now we're ready to take our columnWidth that we read in and calculate a columnCount from it. Our columnCount will be screenWidth divided by columnWidth. Now that we have mapped our columnWidth into our columnCount, we can return our columnCount.
Now that we have our columnCount, we can initialize our GridLayoutManager so let's go to the top of the file and let's add in a private class member for GridLayoutManager and we'll call our variable name manager. Copy that and go back down here and call the constructor for GridLayoutManager. We're going to pass in a context.
Every view has a getContext, so I'm going to use that and we're going to pass in the columnCount that we just calculated. Okay, so one last thing we need to do is make one call in the onMeasure. The onMeasure is a standard framework call that's available to every view, so let's create that here. OnMeasure takes widthSpec and heightSpec.
In our onMeasure call what we're going to do is call the super and pass in our width and height and we're also going to check on our columnWidth. If we have a columnWidth of zero, then we need to call the GridLayoutManager to set the span count. We're going to initialize our spanCount to the measured width from this method call and divide that by calling width.
And then we make the call to GridColumnLayout manager's set spanCount. So, I'm just going to correct my spelling error here and correct it to spanCount. Okay, so now we are done with creating our custom GridRecyclerView.
- Creating a custom layout with XML
- Setting up OnClick handlers
- Making API calls
- Creating custom views
- Implementing ViewHolder objects
- Signing certificates
- Adding a checkbox