Android The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views.
In this tutorial we are going to discuss following topics:
We should use RecyclerView widget when we have data collections whose elements change at runtime based on user action or network events.
RecyclerView introduces an additional level of abstraction between the RecyclerView.Adapter and RecyclerView.LayoutManager to be able to detect data set changes in batches during a layout calculation. This saves LayoutManager from tracking adapter changes to calculate animations. It also helps with performance because all view bindings happen at the same time and unnecessary bindings are avoided.
To use the RecyclerView widget, we must add these 2 libraries. The first one is appcompat-v7, which ensures that the material styles are backward compatible and second one is the Design Support Library which will allows us to use RecyclerView.
Open your build.gradle of modeule app and add support library dependency, something like below given code.
Customer is my model class, which I am going to use for demonstration.
To customize Android RecyclerView we need to do 2 things:
Here is screenshot, how above layout looks like:
To implement Custom adapter for Android RecyclerView, first we need to Write a ViewHolder class say MyViewHolder.java that extends RecyclerView.ViewHolder.
Now, write a class say CustomerAdapter.java that extends RecyclerView.Adapter
Handling click event of Android RecyclerView is not as easy as it is with Android ListView.
To handle click event of Android RecyclerView create a class say RecyclerViewOnItemClickListener and implement RecyclerView.OnItemTouchListener.
Now add the onItemTouchListener for recyclerView as given below:
In this example, we are going to demonstrate how to use Android RecyclerView with Image and Text.
Step 1: Create a new project in Android Studio and name it Android RecyclerView.
Step 2: Open res -> layout -> activity_main.xml and add following code
Step 3: Open res -> values -> strings.xml and replace it with below given code.
Step 4: Open your res ->values ->colors.xml and replace it's code with below given code of my colors.xml as follows:
Step 5: Create a new layout resource under res -> layout named customer_row.xml. Replace the code of customer_row.xml with below given code.
Step 6: Create a class Customer.java and replace it's code with below given code of my Customer.java as follows:
Step 7:Create a class MyViewHolder.java and replace it's code with below given code of my MyViewHolder.java as follows:
Step 8:Create a class CustomerAdapter.java and replace it's code with below given code of my CustomerAdapter.java as follows:
Step 9:Create a class RecyclerViewOnItemClickListener.java and replace it's code with below given code of my RecyclerViewOnItemClickListener.java as follows:
Step 10: Open your MainActivity.java and replace it's code with below given code of my MainActivity.java as follows:
Step 11: Clean and run the project to see the result.