Search This Blog

Support my cause by downloading

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

 <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:id="@+id/idRecyclerView"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   tools:context="com.recyclerview.MainActivity" >  
 </android.support.v7.widget.RecyclerView>  

Steps 2- Create Activity and RecyclerView.Adapter

MainActivity.java

 package com.recyclerview;  
 import android.os.Bundle;  
 import android.support.v7.app.ActionBarActivity;  
 import android.support.v7.widget.LinearLayoutManager;  
 import android.support.v7.widget.RecyclerView;  
 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" };  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           mRecyclerView = (RecyclerView) findViewById(R.id.idRecyclerView);  
           LinearLayoutManager mLinearManager = new LinearLayoutManager(this);  
           mRecyclerView.setLayoutManager(mLinearManager);  
           //          StaggeredGridLayoutManager mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL);  
           //          mRecyclerView.setLayoutManager(mStaggeredGridLayoutManager);  
           //  
           //          GridLayoutManager mGridLayoutManager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);  
           //          mRecyclerView.setLayoutManager(mGridLayoutManager);  
           mRecyclerView.setHasFixedSize(true);  
           mRecyclerView.setAdapter(new RecyclerAdapter(mData, this));  
      }  
      @Override  
      public void onItemClicked(int position, RecyclerAdapter mAdapter) {  
           Toast.makeText(this, String.valueOf(position), Toast.LENGTH_LONG).show();  
      }  
 }  

RecyclerAdapter.java
 package com.recyclerview;  
 import android.support.v7.widget.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) {  
                super(v);  
                mTextView = (TextView) v.findViewById(R.id.txt1);  
                mImageView = (ImageView) v.findViewById(R.id.imageView);  
           }  
      }  
      @Override  
      public int getItemCount() {  
           return mDataset.length;  
      }  
      @Override  
      public void onBindViewHolder(ViewHolder arg0, int arg1) {  
           arg0.mTextView.setText(mDataset[arg1]);  
           arg0.itemView.setTag(arg1);  
           arg0.itemView.setOnClickListener(new OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                     mOnItemRecycleViewClickListener.onItemClicked(Integer.parseInt(v.getTag().toString()), RecyclerAdapter.this);  
                }  
           });  
      }  
      @Override  
      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);  
 }  

Android News and source code