Android Coder: A Navigation Drawer RecyclerView

My previous post introduced the new Material Design Navigation Drawer in NixMashup Links for Android. We got as far as replacing the KitKat ListView for holding the categories to using the RecyclerView, so we’ll pick up with that here.

First a look at the RecyclerView-based categories in the Material Design navigation drawer in an Android 5.0 emulator.

Layout Files

Here is the RecyclerView in the Activity Layout XML File followed by the simple TextView layout file for the individual categories.

Adapters, Adapters, Adapters

Using the RecyclerView is all about the adapter, which is why both the base activity class and the navigation drawer helper class implements CategoryAdapter.OnItemClickListener. Below are primary bits from the CategoryAdapter.

We’re extending the RecyclerView.Adapter<> class and creating the OnItemClickListener interface which our activity and drawer helper will use for category onClick(). Our inner ViewHolder class is essentially a ViewGroup that can support not only our category TextView, but other objects we might want to put into each RecyclerView item such as an icon next to the category text. We inflate and create the ViewHolder based on the TextView Layout XML File above in onCreateViewHolder(). 

Initializing the RecyclerView

We initialize the RecyclerView in the NavigationHelper class shown below. As you can see, a RecyclerView needs a Layout Manager to handle its display. We set the Layout Manager and Adapter in the bottom two lines of the NavigationHelper.initView(activity) method.

Android Coder Notes for this Post

The RecyclerView in the Material Design Navigation Drawer can be found in the project’s GitHub repository Version 1.2 Branch.