The RecyclerView is view that we can swipe vertically or horizontally across the screen to slide through views of data.
The RecyclerView is view that we can swipe vertically or horizontally across the screen to slide through views of data.
Unlike the ListView, the RecyclerView is meant to display large amount of data. This is because in a ListView, if there are 50 items you want to display, all 50 items must be ready to go from the start.
Components
There are a couple of components that you will need.
Setting Up
To begin, go into the app level build gradle file and implement the RecyclerView.
For the latest version, visit the android developer website.
Go into a xml layout file and add the RecyclerView. Remember to switch to the Design view. In the palate, search for RecyclerView, and drag the view into the layout.
I am using the Constraint layout so I need to add the constraints. If you are using any other layouts, make sure to set the width and height of the RecyclerView so it will be visible.
Lastly, give the RecyclerView an id.
Writing the Code
Next, go into the class file that is using the layout and create a variable reference for the RecyclerView.
Create the Adapter
Then create the adapter. Define a new class definition and have it extend the Adapter class from RecyclerView.
Then hover over the View Holder class definition, click on the red lightbulb icon, and select the "Create constructor matching super" option.
This will create the constructor that we need to finish the View Holder class.
Once we have basic View Holder is completed, we can place it inside the angle brackets of the adapter.
Then we have to implement the necessary methods for the adapter. Hover over the adapter class definition, click on the red light bulb icon, and the select "Implement Methods" option.
Defining the View for the View Holder
Next, create an xml layout file for the View Holder. Select the Android option from them drop down menu. Right click on the res folder and go to new > Android Resource File.
Give the file a name and select "Layout" for the Resource type.
This layout file will represent the individual views for the RecyclerView.
Swap back to the design view and add 2 TextViews.
Defining a Class for our Data
For this class, we will have 2 String variables – title and subtitle. Then define a constructor and mutator methods for each variable.
Putting it all together
Go back to the class file where you have your adapter. Define an ArrayList for the data class and populate it with some items.
In the onCreateViewHolder() method of the adapter, we want to return a new view holder object.
In the getItemCount() method, return the size of the array with our data.
In the ViewHolder class, declare a reference for the 2 TextViews in the view holder layout.
Then create a method call link(). For the parameter, it will take in a news article object.
In the onBindViewHolder() method of the adapter, call the link() method and pass in a news article object from the array using the position argument.
Then add the adapter to the RecyclerView. Call the setAdapter() method and pass in the adapter.
Changing the Orientation
To change the orientation to horizontal, we can call the setOrientation() method from the LinearLayoutManager and set it to horizontal.
linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);