Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement multiple filtering on RecyclerView based android app?

Currently, I have developed an Activity to show list of data using RecyclerView with searching feature (as seen at image_1).

I want to add filtering option to the toolbar similar to the filter on the Youtube app (see image_2). Can anyone can give me clues to easily implement it? Both for the UI design and business logic.

Any idea how to implement the filter dialog window (see image_3)? Should I just use AlertDialog or is there any other better options?

image_1 image_2 image_3

like image 297
masu.mo Avatar asked Aug 22 '16 14:08

masu.mo


People also ask

How can use sorting and filter list data in Android?

Create a filter. The "Sort and filter" menu will open. Type in the search bar to find a value or scroll up on the list to see the values in the column. Tap an item to uncheck it and filter it out. The sheet will update automatically.

What is RecyclerView ViewHolder?

A RecyclerView. ViewHolder class which caches views associated with the default Preference layouts. A ViewHolder describes an item view and metadata about its place within the RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive findViewById results.

What is RecyclerView setHasFixedSize?

setHasFixedSize(true) means the RecyclerView has children (items) that has fixed width and height.

How do I add a filter to my adapter?

In its simplest form, you just create an array adapter passing it a list of objects (that have a proper toString() method). Then you type some characters to the textbox and by default it will filter the results searching in the beginning of the backing object's toString() result.


1 Answers

So, you need to create a options menu that starts a DialogFragment with a custom layout and get the tags filtered by the user in your dialog, then you can use them to filter your recyclerview.

Your adapter should implements Filterable interface!!

You can do it by following theses instructions:

  1. Create a OptionsMenu that starts your DialogFragment. Create a
  2. DialogInterface.OnClickListener to your search button in the dialog
  3. that call your adapter.filter by your tags. Create a
  4. Filter(android.widget.Filter) that receives your tags and then you could filter the data and notify!

Your adapter has to implements Filterable.

How to filter a RecyclerView with a SearchView

like image 139
Thiago Souto Avatar answered Nov 15 '22 03:11

Thiago Souto