Search This Blog

Tuesday 31 March 2015

RecyclerView example, Source code with onItemClickListener 

What is RecyclerView?

Android official site states “The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views ”

RecyclerView is generally used when elements changes at run time on user action or network events

You will need android-support-v7-recyclerview.jar for RecyclerView to integrate in your projects. This article will explain you a way to implement onItemClickListener in RecyclerView.

Lets implement RecyclerView steps by steps. Source code  is here in case you may want to skip theory or unable to find android-support-v7-recyclerview.jar

Steps 1- Create android xml layouts

 < xmlns:android=""  
   tools:context="com.recyclerview.MainActivity" >  

Steps 2- Create Activity and RecyclerView.Adapter

 package com.recyclerview;  
 import android.os.Bundle;  
 import android.widget.Toast;  
 public class MainActivity extends ActionBarActivity implements OnItemRecycleViewClickListener {  
      RecyclerView mRecyclerView;  
      private String[] mData = { "Tofeeq AHmad", "Recycler View", "Layout managers for positioning items", "Default animations for common", "Tofeeq AHmad", "Recycler View",  
                "Layout managers for positioning items", "Default animations for common", "Tofeeq AHmad", "Recycler View", "Layout managers for positioning items",  
                "Default animations for common" };  
      protected void onCreate(Bundle savedInstanceState) {  
           mRecyclerView = (RecyclerView) findViewById(;  
           LinearLayoutManager mLinearManager = new LinearLayoutManager(this);  
           //          StaggeredGridLayoutManager mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL);  
           //          mRecyclerView.setLayoutManager(mStaggeredGridLayoutManager);  
           //          GridLayoutManager mGridLayoutManager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);  
           //          mRecyclerView.setLayoutManager(mGridLayoutManager);  
           mRecyclerView.setAdapter(new RecyclerAdapter(mData, this));  
      public void onItemClicked(int position, RecyclerAdapter mAdapter) {  
           Toast.makeText(this, String.valueOf(position), Toast.LENGTH_LONG).show();  
 package com.recyclerview;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.view.ViewGroup;  
 import android.widget.ImageView;  
 import android.widget.TextView;  
 public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {  
      private String[] mDataset;  
      OnItemRecycleViewClickListener mOnItemRecycleViewClickListener;  
      public RecyclerAdapter(String[] myDataset, OnItemRecycleViewClickListener mOnItemRecycleViewClickListener) {  
           mDataset = myDataset;  
           this.mOnItemRecycleViewClickListener = mOnItemRecycleViewClickListener;  
      public static class ViewHolder extends RecyclerView.ViewHolder {  
           public TextView mTextView;  
           public ImageView mImageView;  
           public ViewHolder(View v) {  
                mTextView = (TextView) v.findViewById(;  
                mImageView = (ImageView) v.findViewById(;  
      public int getItemCount() {  
           return mDataset.length;  
      public void onBindViewHolder(ViewHolder arg0, int arg1) {  
           arg0.itemView.setOnClickListener(new OnClickListener() {  
                public void onClick(View v) {  
                     mOnItemRecycleViewClickListener.onItemClicked(Integer.parseInt(v.getTag().toString()), RecyclerAdapter.this);  
      public ViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {  
           View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, parent, false);  
           ViewHolder vh = new ViewHolder(v);  
           return vh;  

Steps 3- To Implment onItemClickListener create one interface and attach it with adapter. RecyclerAdapter adapter already have this attach with it in step 2.

 package com.recyclerview;  
 public interface OnItemRecycleViewClickListener {  
      public void onItemClicked(int position, RecyclerAdapter mAdapter);  

No comments:

Post a Comment

Feedback always help in improvement. If you have any query suggestion feel free to comment and Keep visiting my blog to encourage me to blogging

Android News and source code