Android Recycler View Example

Recycler view widget is an enhanced version of list view with advanced features. It lets you display large data set efficiently. The efficiency is achieved by reusing item views and not performing or reducing findByView operations.

Recycler view widget contains layout manager, recycler view adapter and animation adapter. Layout manager positions item views within in recycler view. Recycler view adapter provides views, creating them by populating item data in views. Animation adapter provides animation on item changes, for example animation when adding and removing items.

How to use recycler view

To display list of items in your app, you can use recycler view. To use recycler view, you need layout manager, recycler view adapter and animation adapter.

Recycler view layout manager

Layout manager measures and positions views in recycler view. It also decides when to recycle views which are not shown on screen. Support library provides linear layout manager, grid layout manager and staggered grid layout. You can use one of them or create custom layout manager by extending RecyclerView.LayoutManager class.

Recycler view adapter

You need to create recycler view adapter which binds your data to views. Recycler view adapter can be created by extending RecyclerView.Adapter class. Recycler view adapter uses view holder, which you need to create by extending RecyclerView.ViewHolder class, to store item view data and metadata related to its place in recycler view.

Animation adapter

Animation adapter provides animation on items as content change or on move events in recycler view. If you don’t provide animation adapter, default animation adapter, DefaultItemAnimator, is used. You can create custom animation adapter by extending RecyclerView.ItemAnimator class.

Recycler view example

I’ll show you how to use recycler view and how to create recycler view adapter using coupons app. In this example, list of coupons will be displayed in recycler view. Each coupon contains data such as store name, coupon text and expiration date.

Recycler view event handling

In this coupon example, coupon item click events are handled by event listener. Coupon view holder in recycler adapter implements View.OnClickListener and sets the listener to item view. When item view is clicked, this listener’s onClick method gets called.

Recycler view adapter output

android recycler view example

Recycler view adapter code


 package com.zoftino.materialdesign;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

public class RecyclerViewActivity extends AppCompatActivity {

    private RecyclerView couponRecyclerView;
    private RecyclerView.Adapter couponAdapter;
    private RecyclerView.LayoutManager couponLayoutManager;

    protected void onCreate(Bundle savedInstanceState) {

        Toolbar myToolbar = (Toolbar) findViewById(R.id.z_toolbar);

        couponRecyclerView = (RecyclerView) findViewById(R.id.all_coupons);

        couponLayoutManager = new LinearLayoutManager(this);

        couponAdapter = new CouponRecyclerViewAdapter(CouponStoreData.couponsData,this);



Activity layout






 package com.zoftino.materialdesign;

public class Coupon {
    private String storeName;
    private String coupon;
    private String expiarationDt;

    public String getStoreName() {
        return storeName;

    public void setStoreName(String storeName) {
        this.storeName = storeName;

    public String getCoupon() {
        return coupon;

    public void setCoupon(String coupon) {
        this.coupon = coupon;

    public String getExpiarationDt() {
        return expiarationDt;

    public void setExpiarationDt(String expiarationDt) {
        this.expiarationDt = expiarationDt;

Recycler view adapter

 package com.zoftino.materialdesign;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class CouponRecyclerViewAdapter extends RecyclerView.Adapter<CouponRecyclerViewAdapter.ViewHolder> {

    private List<Coupon> couponList;
    private Context context;

    public CouponRecyclerViewAdapter(List<Coupon> couponLst, Context ctx){
        couponList = couponLst;
        context = ctx;

    public CouponRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.coupon_row, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;

    public void onBindViewHolder(ViewHolder holder, int position) {
        Coupon cpn = couponList.get(position);

    public int getItemCount() {
        return couponList.size();

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        public TextView storeName;
        public TextView coupon;
        public TextView expirationDt;

        public ViewHolder(View view) {
            storeName = (TextView) view.findViewById(R.id.storeName);
            coupon = (TextView) view.findViewById(R.id.coupon);
            expirationDt = (TextView) view.findViewById(R.id.expirationDt);
        public void onClick(View v){
            storeName = (TextView) v.findViewById(R.id.storeName);
            Toast.makeText(context, "You selected "+storeName.getText()+" coupon "+ getAdapterPosition(),

Recycler view item layout