In this tutorial, we are going to learn, how to add search functionality to RecyclerView. RecyclerView is a great improvement over ListView. But when a RecyclerView is flooded with a large dataset, it gets necessary for some sort of filter mechanism that filters that data set according to user requirement. In this tutorial, we will implement search over RecyclerView dataset, such that it shows a list of data that contains the characters or words typed by the user.
1. You know how to implement RecyclerView in Android. If you don’t know, I will be implementing all the code required for RecyclerView, but it would be better if you first understand how to use RecyclerView.
2. Add below-given dependency to app-level build.gradle file.
1. First of all, create the layout for your activity with a RecyclerView and an EditText (used for taking search string from the user).
2. I am naming my activity layout file as activity_recycler_search.xml. The code for the same is given below.
3. Place the below-given codes in their respective files as specified in comments. These files are required in the above-given layout file.
I have used EditText for taking input from the user, but you can also use SearchView in ToolBar based on your requirement. If you want to use SearchView, then you just need to implement SearchView instead of EditText and rest of the code remain same.
1. Now before writing the code for RecyclerView Adapter, we need a POJO (Plain Old Java Object) class and a layout file for the views inside the RecyclerView.
2. We use POJO class as a model for each item in our RecyclerView dataset. The code for POJO class and view layout file is given below.
1. Now we will define the code for our Adapter class.
2. The code for Adapter class is same as any other RecyclerView Adapter class. It just implements a Filterable Interface which is required for filtering the dataset according to a user query and also it defines two lists, one contains the original dataset and other contains the filtered dataset.
3. The code for the Adapter class is given below, with an explanation.
Our Adapter implements a Filterable interface, and which need to override the getFilter() method. In the constructor we are updating our both Lists (Original dataset and Filtered dataset) with the dataset we receive as a parameter.
In the getFilter() method we return a Filter object. The Filter object overriding performFiltering() and publishResults() method. In the performFiltering() method we get the text, we search via EditText. We loop through the original Dataset and check if it contains the string using contains() method and adds it to a filtered Dataset. In the publishResults() we convert the FilterResults object to the ArrayList object and notifyDataSetChanged() is called to update the adapter.
If you are getting confused with Update method defined in above code, then it is nothing to do with filter functionality. It is rather used to update the dataset of our adapter with the data received from network call (if you are fetching data from the Internet).
1. Now we will write the code for our Activity class.
2. In this class, we are instantiating our Adapter and attaching it to the RecyclerView.
3. We have also defined a onTextChanged method on the EditText to take the text from the user and pass it to the getFilter() method implemented in our Adapter class.
4. The code for the same is given below.
In above activity class, I have left fetchData() method without code. You should change it with your code to fetch data from the source of your requirement. Or if you are not fetching the data from Internet, then just pass the local data to the Adapter.
1. I have written all the necessary code required for attaching Search to RecyclerView. Now you just need to run the app and see it happening.
I have tried to give a basic understanding of how to have SEARCH on RecyclerView. Now it depends on you, how do you want to implement the code apart from the Search Functions (Filterable Interface).
If you face any problem in above tutorial, feel free to contact me through the Chat Option given in bottom right corner of the window.