Android RecyclerView List Example

The RecyclerView class extends the ViewGroup class and implements ScrollingView interface. It is introduced in Marshmallow. 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.

In this tutorial, we will create a list of items with ImageView (for the icon) and TextView (for description) using RecyclerView and performs click listener on the item of its list.

Android RecyclerView with List Example

Create an Android project, and add the RecyclerView support library com.android.support:recyclerview-v7:23.1.0 or above this version in build.gradle file.

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

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.v7.widget.RecyclerView  
  3.         xmlns:android="http://schemas.android.com/apk/res/android"  
  4.         xmlns:tools="http://schemas.android.com/tools"  
  5.         android:layout_width="match_parent"  
  6.         android:layout_height="match_parent"  
  7.         android:scrollbars="vertical"  
  8.         android:id="@+id/recyclerView"  
  9.         tools:context="example.javatpoint.com.recyclerviewlist.MainActivity">  
  10.   
  11. </android.support.v7.widget.RecyclerView>  

Create a dimens.xml file in values directory, and add the following code.

dimens.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <dimen name="activity_horizontal_margin">16dp</dimen>  
  4.     <dimen name="activity_vertical_margin">16dp</dimen>  
  5.     <dimen name="ic_clear_margin">56dp</dimen>  
  6. </resources>  

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

list_item.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/relativeLayout"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="?android:attr/listPreferredItemHeightLarge"  
  6.     android:background="@drawable/border">  
  7.   
  8.     <ImageView  
  9.         android:id="@+id/imageView"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_centerVertical="true"  
  13.         android:layout_alignParentStart="true"  
  14.         android:layout_alignParentLeft="true"  
  15.         android:layout_marginStart="@dimen/activity_horizontal_margin"  
  16.         android:layout_marginEnd="@dimen/activity_horizontal_margin"  
  17.         android:contentDescription="Icon" />  
  18.   
  19.     <TextView  
  20.         android:id="@+id/textView"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="match_parent"  
  23.         android:layout_toEndOf="@id/imageView"  
  24.         android:layout_toRightOf="@id/imageView"  
  25.         android:gravity="center_vertical"  
  26.         android:textSize="16sp"/>  
  27.   
  28. </RelativeLayout>  

Create a border.xml file in the drawable directory which is used to decorate the border of RecyclerView items.

border.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="rectangle">  
  4.     <solid android:color="#FFFFFF" />  
  5.     <stroke  
  6.         android:width="1dp"  
  7.         android:color="#CCCCCC" />  
  8. </shape>  

Create a MyListData.java class with the following code. This class is used as (POJO) class which sets the properties of the items.

MyListData.java

  1. package example.javatpoint.com.recyclerviewlist;  
  2. public class MyListData{  
  3.     private String description;  
  4.     private int imgId;  
  5.     public MyListData(String description, int imgId) {  
  6.         this.description = description;  
  7.         this.imgId = imgId;  
  8.     }  
  9.     public String getDescription() {  
  10.         return description;  
  11.     }  
  12.     public void setDescription(String description) {  
  13.         this.description = description;  
  14.     }  
  15.     public int getImgId() {  
  16.         return imgId;  
  17.     }  
  18.     public void setImgId(int imgId) {  
  19.         this.imgId = imgId;  
  20.     }  
  21. }  

Create a MyListAdapter.java class and add the following code. This class extends RecyclerView.Adapter class and override its unimplemented methods. The onCreateViewHolder() methods inflates the list_item.xml. In the onBindViewHolder() method each data items are set to each row.

MyListAdapter.java

  1. package example.javatpoint.com.recyclerviewlist;  
  2.   
  3. import android.support.v7.widget.RecyclerView;  
  4. import android.view.LayoutInflater;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.ImageView;  
  8. import android.widget.RelativeLayout;  
  9. import android.widget.TextView;  
  10. import android.widget.Toast;  
  11.   
  12.   
  13. public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ViewHolder>{  
  14.     private MyListData[] listdata;  
  15.   
  16.    // RecyclerView recyclerView;  
  17.     public MyListAdapter(MyListData[] listdata) {  
  18.         this.listdata = listdata;  
  19.     }  
  20.     @Override  
  21.     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
  22.         LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());  
  23.         View listItem= layoutInflater.inflate(R.layout.list_item, parent, false);  
  24.         ViewHolder viewHolder = new ViewHolder(listItem);  
  25.         return viewHolder;  
  26.     }  
  27.   
  28.     @Override  
  29.     public void onBindViewHolder(ViewHolder holder, int position) {  
  30.         final MyListData myListData = listdata[position];  
  31.         holder.textView.setText(listdata[position].getDescription());  
  32.         holder.imageView.setImageResource(listdata[position].getImgId());  
  33.         holder.relativeLayout.setOnClickListener(new View.OnClickListener() {  
  34.             @Override  
  35.             public void onClick(View view) {  
  36.                 Toast.makeText(view.getContext(),"click on item: "+myListData.getDescription(),Toast.LENGTH_LONG).show();  
  37.             }  
  38.         });  
  39.     }  
  40.   
  41.   
  42.     @Override  
  43.     public int getItemCount() {  
  44.         return listdata.length;  
  45.     }  
  46.   
  47.     public static class ViewHolder extends RecyclerView.ViewHolder {  
  48.         public ImageView imageView;  
  49.         public TextView textView;  
  50.         public RelativeLayout relativeLayout;  
  51.         public ViewHolder(View itemView) {  
  52.             super(itemView);  
  53.             this.imageView = (ImageView) itemView.findViewById(R.id.imageView);  
  54.             this.textView = (TextView) itemView.findViewById(R.id.textView);  
  55.             relativeLayout = (RelativeLayout)itemView.findViewById(R.id.relativeLayout);  
  56.         }  
  57.     }  
  58. }  

Finally, in the MainActivity.java class, add the following code. This class creates the array of items for MyListData class and set the adapter class to RecyclerView.

MainActivity.java

  1. package example.javatpoint.com.recyclerviewlist;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.support.v7.widget.LinearLayoutManager;  
  6. import android.support.v7.widget.RecyclerView;  
  7.   
  8. public class MainActivity extends AppCompatActivity {  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);  
  14.         MyListData[] myListData = new MyListData[] {  
  15.                 new MyListData("Email", android.R.drawable.ic_dialog_email),  
  16.                 new MyListData("Info", android.R.drawable.ic_dialog_info),  
  17.                 new MyListData("Delete", android.R.drawable.ic_delete),  
  18.                 new MyListData("Dialer", android.R.drawable.ic_dialog_dialer),  
  19.                 new MyListData("Alert", android.R.drawable.ic_dialog_alert),  
  20.                 new MyListData("Map", android.R.drawable.ic_dialog_map),  
  21.                 new MyListData("Email", android.R.drawable.ic_dialog_email),  
  22.                 new MyListData("Info", android.R.drawable.ic_dialog_info),  
  23.                 new MyListData("Delete", android.R.drawable.ic_delete),  
  24.                 new MyListData("Dialer", android.R.drawable.ic_dialog_dialer),  
  25.                 new MyListData("Alert", android.R.drawable.ic_dialog_alert),  
  26.                 new MyListData("Map", android.R.drawable.ic_dialog_map),  
  27.         };  
  28.   
  29.         RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);  
  30.         MyListAdapter adapter = new MyListAdapter(myListData);  
  31.         recyclerView.setHasFixedSize(true);  
  32.         recyclerView.setLayoutManager(new LinearLayoutManager(this));  
  33.         recyclerView.setAdapter(adapter);  
  34.     }  
  35. }  

Output:

Android RecyclerView List Example Android RecyclerView List Example