Android Recycler View

Supun Ishara Weerasekara
Level Up Coding
Published in
7 min readFeb 8, 2020

--

The advanced enhancement of recyclerView with click listeners.

Android recyclerview is the most advanced version of the listview. basically, an android listview is used to present a simple data set. if you want to display large data set in your app, you should use recyclerview. In this article, I have described features of the recyclerview and how to implement it in an advanced manner with a simple example application.

RecyclerView is a much more powerful, flexible, smooth, and a major enhancement of listview. Previously most of the developers used android Listview and android Gridview. according to the android developer web site, recyclerview is initiated with Android Lolipop, the version is 22.1.0 which is under the support library. and it belongs to Maven artifact (com.android.support:recyclerview-v7:28.0.0-alpha1). the latest version of the RecyclerView (androidx.recyclerview:recyclerview:1.2.0-alpha01) is related to AndroidX .

The Android support library is no longer maintained by the android. However, almost all the features under brought by the support library can be used with the support of AndroidX which is part of Jetpack.

RecyclerView has more attractive, smooth controls, functionality, and reliability over list view items. recyclerview is using a significant design pattern to render the result. There are more advantages than using other views such as ListView, GridViews. Recyclerview is much more customizable than listview and gives a lot of control and power to its developers.

Advantages of using RecyclerView

[1]. Performance on Loading

The major advantage of the usage of RecyclerView is the performance when loading list items in the list to the view. RecyclerView prepares the view behind and ahead beyond the visible entries. It gives significant performance when you need to fetch the bitmap image in your list from a background task. If you use RecyclerView.setHasFixedSize, it will give you a dramatically faster way. ListView is based on the premise that there is no way to precalculate or cache the size of entries in the list until completion of the scrolling.

[2]. ViewHolder Pattern

This is the major difference between the listview and recyclerview. It is recommended to use the ViewHolder design pattern but is not compulsory. However, it is mandatory using RecyclerView.ViewHolder class. When we use listview we faced a lot of problems that are solved in recyclerview and it is a bit more complex than other views’ implementations.

[3]. Layout Manager

This is another major enhancement of the recyclerview than listview. In Listview, there is only one type of view available. There is no optimum way to implement for the horizontal list view. Using recyclerview we can have a different layout with layout managers and can be done all the views dynamically. those are as follows.

  1. LinearLayoutManager — supports for vertical and horizontal lists
  2. GridLayoutManager — supports for Grid view lists. (ex:- Image Gallery)
  3. StaggeredLayoutManager — supports for staggered lists (ex:- Pinterest)

[4]. Item Decoration

In ListViews it is harder to implement by adding borders or dividers and adding different decorations to list items. But in the case of RecyclerView is RecyclerView.ItemDecorator class gives more sophisticated control to the developer. However, it is a bit more time consuming and complex.

[5]. Item Animator

Listview does not give much support for having good animations. but, RecyclerView brings a whole new dimension to it. Using the RecyclerView.ItemAnimator class, animating the views becomes so much easy and intuitive.

[6]. OnItemTouchListener

RecyclerView gives much more powerful control to developers via RecyclerView.OnItemTouchListener than ListView. But it has more implementation to complete by the developer. Although list item click events in ListView can be handled through the AdapterView.OnItemClickListener Interface.

For More Details visit the Android developer web site.

Disadvantages of using RecyclerView

  1. More complex than ListView.
  2. Take much more time to implement than ListView implementation.
  3. It can take a lot of time for a beginner to understand a RecyclerView fully.
  4. It can unnecessarily make your coding life difficult.

Android RecyclerView Implementation

First of all, I should say that the latest version of Android Studio and AndroidX are being used to develop this sample application. In this example, I was trying to explain how to implement Recylerview and how to handle the click event when the list item is clicked. The code is available in GitHub and explained below step by step.

GitHub Link for the repository:

1.) Create a New Project

[1]. In Android Studio go to File → New → New Project. And select the Empty activity and Click the Next button then fill the required details for creating the new project. Finally, press the Finish button.

[2]. Open build.gradle at the app-level and add the dependencies below mentioned.

implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha01'
dependencies of recyclerview in build.gradle at the app-level

[3]. With the latest version of the Android Studio build tool. It will generate a layout fill called activity_main.xml. it contains a CoordinateLayout. I have changed it into RelativeLayout for the sake of developer purposes. (There is no difference within the changes I have done in activity_main.xml).

activity_main.xml

[3]. Under the res directory in Android view in Android Project Structure, There is a value directory. Inside that, it has colors.xml, strings.xml, styles.xml

2.) Create the Adapter Class and View.

After adding the RecyclerView widget we should create an adapter class and view holders to render the data. The RecyclerView adapter is the same as the ListView adapter but it is extended with RecyclerView.Adapter. Code snippets are below mentioned.

[1]. Create a model class for data manipulation with the getters and setters.in here I have named the class as DataModel

DataModel.java

[2]. Create a layout for list items. It is used to represent each item in the list separately. Each and every item on the list has used this view. Here I have added the First Name, Last name, Contact No and Id in the view named the filed as recyclerview_list_item.xml

recyclerview_list_item.xml

[3]. Create a class name RecyclerViewAdapter with ViewHolder by extending RecyclerView.Adapter. Especially I should mention here, there are some important methods here such as onCreateViewHolder which inflates the layout file into view, onBindViewHolder that is set data to each view holder in the view and getItemCount() which is used to get the number of items available in the list. As well, I have added the constructor of the class with the List object of DataModel class as a parameter.

//create constructor with list
public RecyclerViewAdapter(List<DataModel> dataModelList) {
this.dataModelList = dataModelList;
}

By extending RecyclerView.ViewHolder class I have created RecyclerViewHolder as an inner class of adapter class.

// declare view holder inner class
class RecyclerViewHolder extends RecyclerView.ViewHolder {
TextView tvID;
TextView tvFirstName;
TextView tvLastName;
TextView tvContactNo;
LinearLayout llItemView;

public RecyclerViewHolder(View itemView) {
super(itemView);
llItemView = itemView.findViewById(R.id.llItemView);
tvID = itemView.findViewById(R.id.tvID);
tvFirstName = itemView.findViewById(R.id.tvFirstName);
tvLastName = itemView.findViewById(R.id.tvLastName);
tvContactNo = itemView.findViewById(R.id.tvContactNo);
}
}

Do not forget to inflate the layout file for a list item in onCreateViewHolder method

The adapter class code snippet is as follows.

RecyclerviewAdapter.java

Then Open the MainActivity.java and create the private list with DataModel object.

private List<DataModel> dataModelList;

Set data to list

private void setData() {
dataModelList = new ArrayList<>();
dataModelList.add(new DataModel(1, "Supun", "Ishara", "+94711234567"));
dataModelList.add(new DataModel(2, "Jhon", "Smith", "+11711234567"));
dataModelList.add(new DataModel(3, "Alexa", "Mori", "+23711234567"));
dataModelList.add(new DataModel(4, "Kasun", "Weerasekara", "+61711234567"));
dataModelList.add(new DataModel(5, "Ishara", "De Silva", "+91711234567"));
dataModelList.add(new DataModel(6, "Sanath", "Weerawarana", "+67711234567"));
dataModelList.add(new DataModel(7, "Jhone", "English", "+41711234567"));
dataModelList.add(new DataModel(8, "April", "Beauty", "+32711234567"));
}

Then initiate the view and adapter in onCreate() method

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Instantiate the recyclerView
recyclerView = findViewById(R.id.recyclerView);

recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setFocusable(false);
setData();
setDataToRecyclerView();
}

Finally set data to adapters

private void setDataToRecyclerView() {
RecyclerViewAdapter adapter = new RecyclerViewAdapter(dataModelList);
recyclerView.setAdapter(adapter);
}

3.) Implement click listener and its usages.

When clicking the list item in the RecyclerView, it has to be managed the click event. Therefore we need to listen to the click event so we implemented the click RecyclerTouchListener with the onItemTouchListner so that each and every clack event triggers the recyclerview and its list items.

The architecture of the code

Here I have created the RecyclerViewClickListener for transfer information to notify which item is clicked.

package com.androidapp.supunisharaweerasekara.recyclerviewapp.interfaces;

import android.view.View;

public interface RecyclerViewClickListener {
void onClick(View view, int position);
}

Then add the implementation on RecyclerTouchListener

RecyclerTouchListener.java

How to Integrate RecyclerTouchListener on recyclerView.

We need to call the recyclerview object and addOnItemTouchListener with dot operation. In my example, I have created another activity to pass the data and showing on the new activity when clicking the recyclerview list item. Here you can see the implementation.

recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, recyclerView, new RecyclerViewClickListener() {
@Override
public void onClick(View view, int position) {
String firstName = dataModelList.get(position).getFirstName();
String lastName = dataModelList.get(position).getLastName();
String contactNo = dataModelList.get(position).getContactNo();
int id = dataModelList.get(position).getId();
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.putExtra("firstName", firstName);
intent.putExtra("lastName", lastName);
intent.putExtra("contactNo", contactNo);
intent.putExtra("id", id);
MainActivity.this.startActivity(intent);
}
}));

I have created another activity called ResultActivity and it shows the data coming from the main activity.

ResultActivity.java

That’s all now... I know it’s limited content for the specific example. I hope to update more articles with details soon with different examples and applications. I will update this page with the new article links.

Thanks for reading this, I hope you liked it, give a clap..

--

--

Senior Software Engineer, Full Stack Mobile Developer, MSc in IT Specialized in Mobile Programming and Cybersecurity, BSc In IT