I, Preamble
- Hello everyone, today I will introduce to you how to implement SearchView with many filters using RxJava in Android with my experience.
- This is also the last article in the series Create 1 Search app complete with SearchView, RxJava and its data binding .
- If people have not refer to their previous posts, you can refer to:
- Taking advantage of the results achieved in part 1 and part 2, we will do this part 3.
II, The problem and how to do it
1, The problem
- We have 2 sources of data returned from the server (similar to the database) to display together when the user searches.
- Searching for Tag by repo.getTags () return Observable <List <Tag >> and search Post by repo.getPosts () return Observable <ListPost >>.
- For each word the user fills in the SearchView will produce the corresponding tags and posts.
- Filter condition: you can search by keyboard or tag (search results).
2, How to do it
- In order to solve this problem, we need 2 important steps if we only choose to use 1 Adapter:
- Step 1 : Create a wrap object – SearchResult to wrap 2 Tag and Post objects. Each SearchResult can contain 1 List of tags or only 1 Post
- Step 2 : Based on the terms: Use the word Zip to combine 2 data sources to have a SearchResult list displayed for the user.
- To better understand what people can observe the image below:
- Note : You can also use MergeAdapter when you intend to use multiple Adapters but I will not give in this article.
- Step 1: Create combine object – SearchResult.
- Tag object:
- Post object:
- SearchResult object:
- Step 2: Zip 2 data sources according to filter conditions:
- Note : Do not forget that each time the user searches we need to recognize when the user searches with the keyboard and when to search with the tag.
III, Summary
- Above is my experience when doing RxJava search with many filters.
- With Android, we also have some other tools and directions to solve this problem such as MergerAdapter, coroutine …
- It has been a long time since there has been no new knowledge for everyone, so the following article will share carefully every corner of power tool / design: coroutine .
- There may be many people who have read through and know about this problem but I still want to dig into every corner of it.
- Happy coding!