Introduction
RecyclerView is the advanced version of ListView in Andriod. In this article, we will learn about RecyclerView in Android with Java programming language.
RecyclerView in Android
RecyclerView is the advanced version of ListView in Android. RecyclerView is used to show the data in the form of a scrollable list. It is a ViewGroup to display a large set of data. For each item in a large dataset, it displays a View. RecyclerView is an efficient way to implement a scrollable list.
RecyclerView is a ViewGroup that arranges our views on the screen like LinearLayout, RelativeLayout, etc. It shows the data on the screen in the form of a list. The views in the list are represented by objects with view holder. Such objects are instances of a class you define. Each view holder is responsible for showing a view of a single object.
Why use RecyclerView on Android?
One reason is so we can create a List through the LinearLayout, and the orientation can be vertical. For example, in the Gmail application, all emails will be rendered as many views in our inbox. Now if we will take LinearLayout, all the mail will create separate views in the memory, but only a few emails will be shown on the screen of the device. This is a waste of memory.
Data = ["item1", "item2", "item3",........"item500"]
For each item, we have a view, but if we create a separate view holder for each item, we can only see 10-15 views at a time. This will also be a waste of memory.
RecyclerView creates only the number of views that can be shown on the screen of the device because it recycles the views. RecyclerView is used to overcome the problem of memory waste.
So, RecyclerView provides an efficient way to create a scrollable list. For example, if we have data for 500 items.
How does RecyclerView work in Android?
As we know, RecyclerView recycles the views. We will understand how RecyclerView works internally through the image given below.

Explanation
As we saw in the first device of the image, there are 5 items visible at a time. So, we know that the recycler view recycles the views. When the user scrolls the screen, the first view from the top will be removed, and it will be shifted below the last one.
Now, we will see the example code of RecyclerView in Android step-by-step.
Step 1. First, we will create an Android Studio project named RecyclerView Example. Android Studio will create two files in the project: MainActivity.java and activity_main.xml.
Step 2. Before starting with RecyclerView, we have to add the RecyclerView dependency in our build.gradle code file of the application. Here is our build.gradle file.
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
defaultConfig {
applicationId "manigautam.apps.recyclerviewexample"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation "androidx.recyclerview:recyclerview:1.1.0" //RecyclerView depedency
implementation "androidx.recyclerview:recyclerview-selection:1.1.0-rc01"
} After adding the RecyclerView dependency to our project, sync the project.
Step 3. Now, we will add the RecyclerView layout in our activity_main.xml file. Here is our activity_main.xml file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout> Step 4. Now, we will make an xml file in the drawable folder of the project named border_file.xml, which we will use to make the border of the RecyclerView. Here is the code of this file.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:color="@color/colorAccent" android:width="2dp"/>
<corners android:radius="5dp" />
</shape> Step 5. Now, we will create a custom row layout that will represent the layout of every single element displayed in the RecyclerView. We create a layout file named row_layout.xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/image"
android:layout_width="300px"
android:layout_height="300px"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:src="@color/colorPrimaryDark" />
<TextView
android:id="@+id/description"
android:layout_width="250dp"
android:layout_height="55dp"
android:layout_toRightOf="@id/image"
android:layout_marginLeft="15dp"
android:layout_marginTop="35dp"
android:text="abcd"
android:textColor="#000"
android:textSize="30sp" />
</RelativeLayout>


bobPosted Dec 2, 2023, 3:41 PM
What is View_Holder and where is it defined? The closest thing to it is an ABSTRACT class ViewHolder which "can not be instantiated"