How to create a RecyclerView in Android


Step1 : MainActivity.xml
  1. Search for RecyclerView in Palette and drop it into the layout.
  2. Click ok to add the add the dependency
  3. Add an id. Select Width and Height to "match_parent".
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView android:id="@+id/rvMyList" android:layout_width="match_parent" android:layout_height="match_parent"/></FrameLayout>

Step 2 : Create a layout for holding each item in the RecyclerView 
  1. Right click on the res > layout folder
  2. click new Layout Resource File
  3. Give the name recyclerview_item  and set the parent as FrameLayout.
  4. set the width="match_parent" , height="wrap_content" , padding="16dp"
  5. Add a TextView 
  6. Set id="tvItem" , textSize="30sp".
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp">
<TextView android:id="@+id/tvItem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" /></FrameLayout>

Step 3: Create a RecyclerViewHolder class extend it to RecyclerView.ViewHolder class

Recycler view uses ViewHolder to generate each view as per needed

RecyclerViewHolder.java
  1. Extend the class with RecyclerView.ViewHolder
  2. Implement the methods
  3. Get the View and Inflate the layout
  4. Create a method bind and set the bind method for getting each items.

package com.example.recyclerviewdemo;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

class RecyclerViewHolder extends RecyclerView.ViewHolder {
    private TextView tvItem;

    /* Constructor */
    RecyclerViewHolder(@NonNull View itemView) {
        super(itemView);
        tvItem = itemView.findViewById(R.id.tvItem);
    }

    void bind(String item){
        tvItem.setText(item);
    }

}
Step 4: Create ViewHolderAdapter class 
  1. Extend class with RecyclerView.Adapter<RecyclerViewHolder> class
  2. And update as following

  package com.example.recyclerviewdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class RecyclerViewAdapter extends RecyclerView.Adapter {

    private int length;

    public RecyclerViewAdapter(int numberOfItems){
        length = numberOfItems;
    }
    @NonNull
    @Override
    public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.recyclerview_item,parent,false);
        return new RecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
        holder.bind(String.valueOf(position));
    }

    @Override
    public int getItemCount() {
        return length;
    }
}




Comments