Android RecyclerView is one the two UI widgets introduced by the support library in Android Lollipop. In this post I will describe how we can use it and what’s the difference between Android RecyclerView and ListView.
This new widget is more flexible that the ListView but introduces some complexities. As we are used RecyclerView introduces a new Android Adapter that must be used to represent the underlying data in the widget. This new adapter is called RecyclerView.Adapter. To use this component 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.
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: Android RecyclerView and ListView Adapter
If we want to show the information in a ListView or in the new Android 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:
[java]public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyHolder> {….
}[/java]
where MyHolder is our implementation of ViewHolder pattern. We can suppose, we have a simple row layout in our RecyclerView:
[xml]<LinearLayoutxmlns: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>[/xml]
so our ViewHolder pattern implementation is:
[java]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);
}
}[/java]
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 works 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:
[java]@Overridepublic 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;
}[/java]
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:
[java]@Overridepublic 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);
}[/java]
Notice that we don’t make a lookup but we simply use the UI widget reference stored in our view holder.
Android RecyclerView
Now we have our adapter we can create our RecyclerView:
[java]RecyclerView rv = (RecyclerView) findViewById(R.id.my_recycler_view);rv.setLayoutManager(new LinearLayoutManager(this));
MyRecyclerAdapter adapter = new MyRecyclerAdapter(createList());
rv.setAdapter(adapter);[/java]
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:
The most interesting aspect is how many times the onCreateViewHolder is called compared to the number of items shown.
Thanks for this. What replaced setOnItemClickListener() ? How does one detect clicks?
Experiment with Recycler View
https://github.com/rahulrj/Swipe_RecyclerView
Hi, great article. What does the “SO” stand for when you write “while the second method is called when the SO binds the view with the data…”?
It’s just a typo due to the fact that the author is italian. SO stands for “Sistema Operativo”, the italian for Operating System.
Thx for your support!
Thx for spotting it. As said SO stands for OS! 🙂
Thank you all. Regards!
Hi, great article. What does the "SO" stand for when you write "while the second method is called when the SO binds the view with the data…"?
It's just a typo due to the fact that the author is italian. SO stands for "Sistema Operativo", the italian for Operating System.
Thx for spotting it. As said SO stands for OS! 🙂
Thx for your support!
Thank you all. Regards!