An Idiot’s guide to Android RecyclerView Part 1

sanjeev yadav
2 min readJun 16, 2017

--

Step by step tutorial on how to define and use RecylerView. Here’s the end preview of what we will make.

RecyclerView List
  1. Define a Gradle dependencies for RecyclerView in build.gradle file
dependencies {
...
compile 'com.android.support:recyclerview-v7:21.0.+'
}

2. In your activity_main.xml define a RecyclerView tag

3. Create a layout file for individual list item, we named it single_list_item.xml

4. Create a class MyListAdapter and inside it create a inner class MyViewHolder that extends RecyclerView.ViewHolder

/*MyViewHolder class is a place where we will define and reference all the Views defined in our single_list_item.xml file*/

5. Define a TextView instance in MyViewHolder class and reference it using findViewById function inside the constructor

6. Now extend your MyListAdapter class to RecyclerView.Adapter<MyListAdapter.MyViewHolder> and implement all the method by clicking on red bulb appearing on Android Studio

7. Accepting String[] array of list item inside MyListAdapter constructor and completing above todos

// TODO: (1) Send dummy string[] array of list that need to be displayed from MainActivity to MyListAdapter via constructor
// TODO: (2) Set the layout for individual list item inside onCreateViewHolder
// TODO: (3) Return the number of list item in getItemCount
// TODO: (4) Set the data from String[] array to each list item

8. Lastly let’s bind all the things together into MainActivity.java

// TODO: (1) Create a dummy String[] array of data to be sent
// TODO: (2) Create a global variable for RecyclerView and MyListAdapter
// TODO: (3) Inside onCreate get a reference to the RecyclerView using findViewById and passing the id in it
// TODO: (4) Instantiate MyListAdapter object and pass the dummy data in it
// TODO: (5) Define a LinearLayoutManager object that decide how to display list item, i.e., in list, grid or staggered form
// TODO: (6) Hook LinearLayoutManager and MyListAdapter to your RecyclerView

Phew! that was a lot. The motive of this article was to have a handy guide to look back whenever you feel stuck. Just bookmark it to have a handy reference. Next step:

Please do share and like this article if found beneficial and tell me what next Step by step tutorial you want to see next.

--

--

sanjeev yadav

Front end & Android Developer, in deep love with learning about CS and a travel enthusiast :) https://github.com/alexakasanjeev