Join Albert Lo for an in-depth discussion in this video Define BaseViewHolder, part of Android Development: Modern Android UI Design Elements.
- [Narrator] Now that we have our model defined, we are ready to create the class that will manage access to our models and that will be the RecyclerView adapters. But before we create the RecyclerView adapters, let's first create an abstract base class that will be used by our RecyclerView adapters. This abstract base class is called BaseViewHolder and its purpose is to define the structure that all view holders must follow. This also allows the RecyclerView adapter to work with a generic abstract base class instead of specific view holders for albums or photos.
Okay, so let's go ahead and create our BaseViewHolder by going to our package and under our package, we're going to go under UI Google photos and we're going to right mouse, create a new package and we're going to call it view holders. So now we have our new package and we will create a new file, new Java class, and we will call it BaseViewHolder and the modifier we're going to select is abstract because we have an abstract base class and press OK.
As an optional step, I'm just going to close off some of our other files here. Okay, so now we have our BaseViewHolder. It's going to be a subclass so it will extend RecyclerView.ViewHolder. We press Control + O and we select the constructor BaseViewHolder, insert a line.
We are going to define an abstract method here called setData. SetData will take three parameters, the first one will be BaseGridAdapter, the second one will be our Google photos item which is our model and the last one will be a position. I'm going to press Command + One to close our project view.
Now we have a better view of our screen. One other method we will add here will be getRootView. It's a protected method. It will return a view. We call it getRootView. And what we will do here is access the RecyclerView.ViewHolder's itemView and just return that.
Okay, so that completes BaseViewHolder. The key part here is setData method. Any view holder classes that extends BaseViewHolder will have to implement setData.
- Creating a custom layout with XML
- Setting up OnClick handlers
- Making API calls
- Creating custom views
- Implementing ViewHolder objects
- Signing certificates
- Adding a checkbox