About Francesco Azzola

I'm a senior software engineer with more than 15 yrs old experience in JEE architecture. I'm SCEA certified (Sun Certified Enterprise Architect), SCWCD, SCJP. I'm an android enthusiast and i've worked for long time in the mobile development field.

Android RecyclerView

RecyclerView is one of the two UI widgets introduced by the support library in Android L. In this post I will describe how we can use it and what’s the difference between this widget and the “classic” ListView.

This new widget is more flexible than the ListView but it introduces some complexities. As we are used RecyclerView introduces a new Adapter that must be used to represent the underlying data in the widget. This new adapter is called RecyclerView.Adapter. To use this widget you have to add latest v7 support library.
 
 
 

Introduction

We know already that in the ListView to increase the performance we have to use the ViewHolder pattern. This is simply a java class that holds the references to the widget in the row layout of the ListView (for example TextView, ImageView and so on). Using this pattern we avoid to call several times findById method to get the UI widget reference making the ListView scrolling smoother. Even if this pattern was suggested as best-practice we could implement our Adapter without using this pattern.

RecyclerView enforces this pattern making it the core of this UI widget and we have to use it in our Adapter.

The Adapter: RecyclerView.Adapter

If we want to show the information in a ListView or in the new RecyclerView we have to use an Adapter. This component stands behind the UI widget and determines how the rows, in the ListView, have to be rendered and what information to show. Also in the RecyclerView we have to use an Adapter:

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyHolder> {
    ....
}

where MyHolder is our implementation of ViewHolder pattern. We can suppose, we have a simple row layout in our RecyclerView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txt1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txt2"/>

</LinearLayout>

so our ViewHolder patter implementation is:

public static class MyHolder extends RecyclerView.ViewHolder {
    protected TextView txt1;
    protected TextView txt2;

    private MyHolder(View v) {
        super(v);
        this.txt1 = (TextView) v.findViewById(R.id.txt1);
        this.txt2 = (TextView) v.findViewById(R.id.txt2);
    }
}

As you can notice, the lookup process (findViewById) is made in the view holder instead of in getView method.

Now we have to implement some important method in our adapter to make it working properly. There are two important methods we have to override:

  • onCreateViewHolder(ViewGroup viewGroup, int i)
  • onBindViewHolder(MyHolder myHolder, int i)

OnCreateViewHolder is called whenever a new instance of View Holder class must be instantiated, so this method becomes:

@Override
public MyHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    numCreated++;
    Log.d("RV", "OncreateViewHolder ["+numCreated+"]");
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_layout, null);
    MyHolder mh = new MyHolder(v);
    return mh;
}

as you can notice, the method returns an instance of our View holder implementation, while the second method is called when the SO binds the view with the data, so we set the UI widget content to the data values:

@Override
public void onBindViewHolder(MyHolder myHolder, int i) {
    Log.d("RV", "OnBindViewHolder");
    Item item = itemList.get(i);
    myHolder.txt1.setText(item.name);
    myHolder.txt2.setText(item.descr);
}

Notice that we don’t make a lookup but we simply use the UI widget reference stored in our view holder.

RecyclerView

Now we have our adapter we can create our RecyclerView:

RecyclerView rv = (RecyclerView) findViewById(R.id.my_recycler_view);
rv.setLayoutManager(new LinearLayoutManager(this));
MyRecyclerAdapter adapter = new MyRecyclerAdapter(createList());
rv.setAdapter(adapter);

LinearLayoutManager is the “main” layout manager used to dispose items inside the RecyclerView. We can extend or implement our layout manager.

Final considerations

Running the example we obtain:

android_recyclerview[4]

The most interesting aspect is how many times the onCreateViewHolder is called compared to the number of items shown. If you look at the log you will find that the object created is 1/3 of the total number displayed.

Reference: Android RecyclerView from our JCG partner Francesco Azzola at the Surviving w/ Android blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


− two = 3



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close