Recyclerview Introduction:-

The RecyclerView class extends the ViewGroup class and implements ScrollingView interface. It is an advanced version of the ListView with improved performance and other benefits. RecyclerView is mostly used to design the user interface with the fine-grain control over the lists and grids of android application.

RecyclerView is an advanced and flexible version of ListView and GridView. It is a container used for displaying large amount of data sets that can be scrolled very efficiently by maintaining a limited number of views.

RecyclerView was introduced in Material Design in API level 21.

 

Here is the Demo Video of Recyclerview:-


Gradle Dependency to use RecyclerView:-

Gradle Scripts > build.gradle and inside dependencies For Androidx use this  gradle dependency
dependencies {
....
implementation 'androidx.recyclerview:recyclerview:1.0.0'
}
 or
dependencies {
....
implementation 'com.google.android.material:material:1.0.0'
}

In the activity_main.xml file in layout directory, add the RecyclerView widget.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.androidcreation.recycleview.MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

Create a custom layout list_item.xml file with following code.

 

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout"
android:layout_margin="3dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:scaleType="centerCrop"
android:layout_marginEnd="10dp"
android:contentDescription="Icon" />

<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:layout_margin="5dp"
android:layout_toEndOf="@id/imageView"
android:layout_toRightOf="@id/imageView"
android:gravity="center_vertical"
android:textSize="18sp"/>
<TextView
android:id="@+id/textViewAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_toEndOf="@id/imageView"
android:layout_toRightOf="@id/imageView"
android:gravity="center_vertical"
android:textSize="16sp"/>
<TextView
android:id="@+id/textViewDOB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_toEndOf="@id/imageView"
android:layout_toRightOf="@id/imageView"
android:gravity="center_vertical"
android:textSize="16sp"/>
</LinearLayout>

</LinearLayout>

 

Create a MyRecyclerData.java class with the following code. This class is responsible for managing the data of the application. It responds to the request from the view and it also responds to instructions from the controller to manipulate the data

MyRecyclerData.java

package com.androidcreation.recycleview;

public class MyRecyclerData {
private String name,address,dob;
private int imgId;
public MyRecyclerData(String name, int imgId,String address,String dob) {
this.name = name;
this.imgId = imgId;
this.address=address;
this.dob=dob;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name =name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getImgId() {
return imgId;
}
public void setImgId(int imgId) {
this.imgId = imgId;
}

public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
}

Create a MyRecyclerAdapter.java class which extends RecyclerView.Adapter class and overrided methods ,create a constructor for getting the data from Activity. The onCreateViewHolder Method in which we inflate the layout item xml and pass it to View Holder and other is onBindViewHolder in which we set the data in the view’s with the help of ViewHolder.

MyRecyclerAdapter.java

package com.androidcreation.recycleview;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;


public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {
private ArrayList<MyRecyclerData> arrayList;


public MyRecyclerAdapter(ArrayList<MyRecyclerData> arrayList) {
this.arrayList = arrayList;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// infalte the item Layout
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem = layoutInflater.inflate(R.layout.list_item, parent, false);

// pass the view to View Holder
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final MyRecyclerData myRecyclerData = arrayList.get(position);
// set the data in items
holder.textViewName.setText(myRecyclerData.getName());
holder.textViewAddress.setText(myRecyclerData.getAddress());
holder.textViewDOB.setText(myRecyclerData.getDob());

holder.imageView.setImageResource(myRecyclerData.getImgId());

// implement setOnClickListener event on item view.
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// display a toast with person name on item click
Toast.makeText(view.getContext(), "click on item: " + myRecyclerData.getName(), Toast.LENGTH_SHORT).show();
}
});
}


@Override
public int getItemCount() {
return arrayList.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView textViewName,textViewAddress,textViewDOB;
public LinearLayout linearLayout;

public ViewHolder(View itemView) {
super(itemView);
this.imageView = (ImageView) itemView.findViewById(R.id.imageView);
this.textViewName = (TextView) itemView.findViewById(R.id.textViewName);
this.textViewAddress = (TextView) itemView.findViewById(R.id.textViewAddress);
this.textViewDOB = (TextView) itemView.findViewById(R.id.textViewDOB);

linearLayout = itemView.findViewById(R.id.linearlayout);
}
}
}

Finally, in the MainActivity.java class, add the following code.

MainActivity.java

package com.androidcreation.recycleview;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

Integer image[]= {
R.drawable.dwaynejohnson,R.drawable.tom,
R.drawable.jony,R.drawable.robert,
R.drawable.chris,R.drawable.leonardo
};
String name[]={"Dwayne Johnson","Tom Cruise","Johnny Depp","Robert Downey, Jr.","Chris Hemsworth","Leonardo DiCaprio"};
String address[]={"Hayward, California"," Syracuse, New York, United States","Owensboro, Kentucky, United States"
," Manhattan, New York, United States"," Melbourne, Australia"," Los Angeles, California, United States"};
String dob[]={"2 May 1972","3 July 1962"," 9 June 1963","4 April 1965","11 August 1983","11 November 1974"};
ArrayList<MyRecyclerData> arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayList= new ArrayList<>();

for (int i= 0;i<image.length;i++)
{
arrayList.add(new MyRecyclerData(name[i],image[i],address[i],dob[i]));
}


RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
MyRecyclerAdapter adapter = new MyRecyclerAdapter(arrayList);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
}


Output:-

0 CommentsClose Comments

Leave a comment

shares

Newsletter Subscribe

Get the Latest Posts & Articles in Your Email

We Promise Not to Send Spam:)