Friday, 12 December 2014

RecyclerView Tutorial

Android has introduced a new widget for displaying data collections which is a more advanced and flexible version of ListView. RecyclerView incorporates ListView and ViewHolder reusability concept very efficiently.

RecyclerView is very useful with large data collections whose content changes at runtime.

RecyclerView includes:

  1. Layout managers for positioning items. 
  2. Default animatios for common item operations , such as removal or addition of items.

Custom animation and layout managers can be defined as per the requirement.

Layout Manager:

RecyclerView provides 3 built-in layout managers:

  1. LinearLayoutManager shows items in a vertical or horizontal scrolling list. 
  2. GridLayoutManage shows items in a grid. 
  3. StaggeredGridLayoutManager shows items in a staggered grid. 

For custom layout manager, extend the RecyclerView.LayoutManager class.


  1. Animations for adding and removing items are enabled by default.
  2. For custom animation, extend the RecyclerView.ItemAnimator class and use the RecyclerView.setItemAnimator() method.

RecyclerView Demo:

  1. Create a new Android project.
  2. Add the v7 support library, for eclipse follow the link and for gradle add: 
     3.  Add RecyclerView in xml:

     android:scrollbars="vertical" />

   4. Create Adapter file
      by extending RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>

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

 private String[] mData;

 public MyRecyclerViewAdapter(String[] mData) {
  this.mData = mData;

 public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
  View v = LayoutInflater.from(viewGroup.getContext())
    .inflate(R.layout.list_item_layout, viewGroup, false);

  ViewHolder vh = new ViewHolder(v);
  return vh;


 public void onBindViewHolder(ViewHolder viewHolder, int i) {

 public int getItemCount() {
  if (mData != null)
   return mData.length;
  return 0;

 public static class ViewHolder extends RecyclerView.ViewHolder {
  // each data item is just a string in this case
  public TextView mTextView;

  public ViewHolder(View itemView) {
   mTextView = (TextView)itemView.findViewById(;;

   5. Create Activity

public class MyActivity extends Activity implements View.OnClickListener{

    private RecyclerView mRecyclerView;
    private MyRecyclerViewAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private GridLayoutManager mGridLayoutManager;
    private StaggeredGridLayoutManager mStaggeredGridLayoutManager;

    private String[] mSampleData = {"What is Android Lollipop?", "What is Android 5.0?",                     "What is RecyclerView?", "What is difference between RecyclerView and ListView?", "How does this demo app help?"};
    protected void onCreate(Bundle savedInstanceState) {

        mRecyclerView = (RecyclerView) findViewById(;

        mAdapter = new MyRecyclerViewAdapter(mSampleData);

    private void applyLinearLayoutManager(){
        mLayoutManager = new LinearLayoutManager(this);

    private void applyGridLayoutManager(){
        mGridLayoutManager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);

    private void applyStaggeredGridLayoutManager(){
        mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id ==
        else if (id ==
        else if (id ==
        return super.onOptionsItemSelected(item);

    public void onClick(View v) {
        int pos = mRecyclerView.getChildPosition(v);
        Toast.makeText(getApplicationContext(), mSampleData[pos], Toast.LENGTH_LONG).show();

No comments:

Post a Comment