A simple explanation on how to use RecyclerView on Android

Omonayin Bamidele
3 min readOct 21, 2020

Recyclerview is a layout container widget use in displaying list of child views. It has the same function as a scrollview and the list views. One significant advantage of using recyclerview is that it uses the principle of lazy loading, to render only contents that are needed to be displayed on the screen, which in turn leads to increase in performance of the application.
Just as the name implies, the recyclerView recycles its view, instead of recreating it.

A layout using recyclerView to display tasks in a list. The code that generated this view is shown in the post

Recyclerview widget uses both a viewHolder subclass and an adapter subclass. The ViewHolder subclass does one thing: It holds on to a View while adapter subclass is responsible for creating the necessary viewHolder and also binding viewHolders to model.
To create a viewholder subclass, navigate to your application package name in android studio click on it, hover over new and select java class to create a new java file, make sure the class extends RecyclerView.ViewHolder.
This viewholder is the view that will be generated and recycled within a recyclerview.
A typical ViewHolder subclass looks like this:

public class TaskHolder extends RecyclerView.ViewHolder {
CardView cardView ;
TextView spectitle ,dateStr;
ImageView imageView;
CheckBox box;
public TaskHolder(View itemview) {
super(itemview);
cardView = itemview.findViewById(R.id.card);
spectitle = itemview.findViewById(R.id.realtitle);
dateStr = itemview.findViewById(R.id.date_str);
box = itemview.findViewById(R.id.checkBox);
imageView = itemview.findViewById(R.id.imageView);

}


}

RecyclerView does not create ViewHolders itself. Instead, it asks an adapter. An adapter serves as a link between the RecyclerView and the data set that the RecyclerView should display.
An adapter subclass can be created the same way the viewholder subclass was created, but it should be made to extend Recycler.Adapter class which contains the viewholder(Recycler.Adapter<Viewholder>).

This recyclerAdapter subclass has three methods that must be overridden they are.
1.onCreateViewHolder(ViewGroup, int)
2.onBindViewHolder(ViewHolder, int)
3.getItemcount()

The onCreateViewHolder(ViewGroup, int) adapter’s method is called by the RecyclerView. Within this method, the xml layout file (specificview.xml ) that serves as the template for the view we want to display within the recyclerView is inflated and then returned as a viewholder.

@Override
public TaskHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.specificview,parent,false);
return new TaskHolder(view);
}

The onBindViewHolder(ViewHolder, int) method is the method that will be called by the Recyclerview to pull contents needed to fill the view defined within the viewHolder class.

@Override
public void onBindViewHolder(@NonNull TaskHolder holder, int position) {
final Task mBindTask = mTask.get(position);
holder.spectitle.setText(mBindTask.getTitle());
if(mBindTask.getDate() != null){
holder.dateStr.setText(mBindTask.getDate().toString());
}
// holder.box.setChecked(mBindTask.getChecked());

if (holder.box.isChecked()) {
mBindTask.setChecked(true);

}


}

The getItemCount determines the total number of items that the recyclerview will render.

@Override
public int getItemCount() {
return mTask.size();
}

Most times the viewHolder subclass is created as an inner class within an adapter subclass. So the complete code typically looks like this.

public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.TaskHolder>  {
private List<Task> mTask;
private Context context;

public TaskAdapter(List<Task> mTask, Context context) {
this.mTask = mTask;
this.context = context;
}

@NonNull
@Override
public TaskHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.specificview,parent,false);
return new TaskHolder(view);
}

@Override
public void onBindViewHolder(@NonNull TaskHolder holder, int position) {
final Task mBindTask = mTask.get(position);
holder.spectitle.setText(mBindTask.getTitle());
if(mBindTask.getDate() != null){
holder.dateStr.setText(mBindTask.getDate().toString());
}
// holder.box.setChecked(mBindTask.getChecked());

if (holder.box.isChecked()) {
mBindTask.setChecked(true);

}
}

@Override
public int getItemCount() {
return mTask.size();
}

//ViewHolder subclass with the name TaskHolder
public class TaskHolder extends RecyclerView.ViewHolder {
CardView cardView ;
TextView spectitle ,dateStr;
ImageView imageView;
CheckBox box;
public TaskHolder(View itemview) {
super(itemview);
cardView = itemview.findViewById(R.id.card);
spectitle = itemview.findViewById(R.id.realtitle);
dateStr = itemview.findViewById(R.id.date_str);
box = itemview.findViewById(R.id.checkBox);
imageView = itemview.findViewById(R.id.imageView);

}


}
}

The recyclerview widget does not know about the existence of the adapter subclass, to makes use of the adapter subclass we will make use of the setAdapter method of the recyclerView to attach the adapter we created with the recyclerView. This is done in either the activity or the fragment class of the application

RecyclerView cyclerView = findViewById(R.id.cyclerview);
TaskAdapter mAdapter = new TaskAdapter(mListOfTasks,this);
cyclerView.setAdapter(mAdapter);//connection cyclerview and adapter
cyclerView.setLayoutManager(new LinearLayoutManager(this));

At the time of writing this post, to make use of the recyclerView widget you need to add the dependency to your project manually.

--

--