Mobile App development Company UK

Custom & Filterable Adapter

Code optimization is one of the most core features of a good developer. As far as Android developers are concerned the better the optimised code the better is the performance.

Every time creating a new separate class for each adapter for a simple RecyclerView makes the code redundant. 

  1. Create a new adapter class 
  2. Extend with RecyclerView.Adapter
  3. Create another new class
  4. Extend with RecyclerView.ViewHolder
  5. Write logic in onBindView

Except for the point no. 5 all steps are repeatable, that means with every new adapter we need to write codes for all points. So, a Custom Adapter can find a way for this where all points except 5 will be common for all and 5th point will be overridden to write your logic.

  1. CustomAdapter is a generic adapter abstract class which will optimize code and avoid writing redundant code. 
  2. CustomAdapter extends RecyclerView.Adapter and has its own ViewHolder class extending RecyclerView.ViewHolder name as ItemViewHolder.
  3. CustomAdapter has a generic type enclosed in anchor bracket <T> which represents what type of data it will hold and show as a list. For example, data can be a list of Car as ArrayList<Car> cars, list of Person as ArrayList<Person>. So <T> here represents <Car>, <Person> etc
  4. CustomAdapter also takes layout resource id as a constructor on how the items will be mapped from the list of data in UI.
  5. In this class, bindView is an abstract class to fulfil above point no. 5. To write your business logic.

FilterableAdapter is an extension of the CustomAdapter. In other words, giving extra functionality to CustomAdapter.

There are many scenarios where we want to filter the list, in order to achieve this we write full fledged code, performing operations on the list like add, remove clear etc.

The steps goes like : 

  1. Create new adapter class 
  2. Extend with CustomAdapter<T> and Implementing Filterable interface
  3. Create another new class
  4. Extend with RecyclerView.ViewHolder
  5. Write logic in onBindView
  6. Create filter class extending Filter class
  7. Perform the Filter operation on the basis of some filtering predicates like person.age > 50, or person.name.startsWith(“A”) etc.

Same as CustomAdapter, FilterableAdapter will focus on point No. 7 the business logic.

  1. Create an adapter extending FilterableAdapter<T> , T can be any data on which you want to person filtration.
  2. Override performFilterating method which will provide filtration predicate as its parameter and the return type is ArrayList<T> which is a filter list filtered on the basis of filtration predicate from the original list.
  3. Add TextWatcher to search editText and on onTextChange write a line filterableAdapter.getFilter().filter(text). Here filterableAdapter is the object of FilterableAdapter, getFilter is the member method, filter is the member method of Filter class and text is what wrote on editText which we get as parameter in onTextChange method.

 

Thanks for giving your valuable time. Keep reading and keep learning

Leave a Reply