Android: How to Use RecyclerView
RecyclerView has been one of Android’s most important components and appears in the majority of apps. Understanding how to configure its behavior is essential. It was introduced in Lollipop and offers a significant performance improvement over the classic ListView.
In this tutorial we’ll cover:
- Using RecyclerView with a custom layout
- Handling click events
- Customizing RecyclerView’s appearance
Getting started with RecyclerView
RecyclerView uses an adapter to represent data in a list — the same concept ListView used, but taken further with a major performance boost. The adapter extends RecyclerView.Adapter and decouples the UI from the data.
First, add the dependency in your build.gradle:
dependencies {
compile 'com.android.support:RecyclerView-v7:25.3.1'
}
For our data model, we’ll use a simple Contacts class:
public class Contacts {
private String name;
private Date birthday;
public Contacts(String name, Date birthday) {
this.name = name;
this.birthday = birthday;
}
}
Add RecyclerView to your layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</LinearLayout>
Get a reference in your Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView rv = (RecyclerView) findViewById(R.id.recycler_view);
}
Now implement the adapter:
public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.MyViewHolder> {
private List<Contacts> contactsList;
private DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView countryText;
public TextView popText;
public MyViewHolder(View view) {
super(view);
countryText = (TextView) view.findViewById(R.id.contactName);
popText = (TextView) view.findViewById(R.id.birthday);
}
}
public ContactsAdapter(List<Contacts> contactLists) {
this.contactsList = contactLists;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Contacts c = contactsList.get(position);
holder.countryText.setText(c.name);
holder.popText.setText(df.format(c.birthday));
}
@Override
public int getItemCount() {
return contactsList.size();
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row, parent, false);
return new MyViewHolder(v);
}
}
The row layout (row.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/contactName"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/birthday" />
</LinearLayout>
Wire the adapter and set the layout manager:
ContactsAdapter ca = new ContactsAdapter(contactsList);
rv.setAdapter(ca);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(llm);
Item click listener
First, define the listener interface inside the adapter:
public interface OnItemClickListener {
void onItemClick(Contacts item);
}
Update the constructor to accept the listener:
private final OnItemClickListener listener;
public ContactsAdapter(List<Contacts> contactLists, OnItemClickListener listener) {
this.contactsList = contactLists;
this.listener = listener;
}
Assign the listener to each item in onBindViewHolder:
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
final Contacts c = contactsList.get(position);
holder.contactText.setText(c.name);
holder.birthdayText.setText(df.format(c.birthday));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onItemClick(c);
}
});
}
Pass the listener when creating the adapter in your Activity:
rv.setAdapter(new ContactsAdapter(contactsList, new ContactsAdapter.OnItemClickListener() {
@Override
public void onItemClick(Contacts item) {
Toast.makeText(MainActivity.this, "Item Clicked", Toast.LENGTH_LONG).show();
}
}));
You can find the complete source code on GitHub: https://github.com/jamontes79/RecyclerViewExample
This is just a first look at RecyclerView. There’s much more to explore in future posts. Any questions about it?