简体   繁体   中英

How to apply search filter to a recyclerview with categorical filtering?

Right now, I have a recyclerview with items of two categories, and three buttons on the activity which filters them by "all", "design category" and "development category". When a button is clicked, only items of that category is displayed. I also have a search bar, which filters the results by name. After something is typed into the searchbar and the results are filtered, if a categorical button is clicked, it displays all items of that category instead of only items that match the name that is searched.

My only current solution is to clear the searchbar when a button is clicked, but I want to make it so that the searchbar filter still applies after a button is clicked.

HomeActivity.Java:

public class HomeActivity implements RecyclerViewAdapter.RecyclerViewOnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {

//other stuffs 

        final Button allLessonsButton = (Button) findViewById(R.id.allLessonsButton);
        allLessonsButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                onAllLessonsButtonTapped();
            }});
        final Button designLessonsButton = (Button) findViewById(R.id.designLessonsButton);
        designLessonsButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                onDesignLessonsButtonTapped();
            }});
        final Button developmentLessonsButton = (Button) findViewById(R.id.developmentLessonsButton);
        developmentLessonsButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                onDevelopmentLessonsButtonTapped();
            }});

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                adapter.getFilter().filter(newText);
                return false;
            }
        });
    }

    setupRecyclerView();
}

    private void setupRecyclerView() {
        lessonRecyclerView = (RecyclerView) findViewById(R.id.lesson_recycler_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        lessonRecyclerView.setLayoutManager(linearLayoutManager);
    }

    private void onAllLessonsButtonTapped() {
        currentLessons = allLessons;
        adapter = new LessonRecyclerViewAdapter(this, currentLessons, this);
        lessonRecyclerView.setAdapter(adapter);
    }

    private void onDesignLessonsButtonTapped() {
        currentLessons = developmentLessons;
        adapter = new LessonRecyclerViewAdapter(this, currentLessons, this);
        lessonRecyclerView.setAdapter(adapter);
    }

    private void onDevelopmentLessonsButtonTapped() {
        currentLessons = designLessons;
        adapter = new LessonRecyclerViewAdapter(this, currentLessons, this);
        lessonRecyclerView.setAdapter(adapter);
    }

Essentially your users have two different methods to filter the list before displaying them. On one hand, they can choose a category using buttons; or they can type some texts and filter it by searching.

The main problem right now is to discern the two competing methods of filtering. If a user has both clicked a category and typed a query, we don't know which filter method we should respect.

In my opinion, we should always respect the latest action.

One way to solve this problem is, for example, have a variable recording the latest action the user had. Example pseudocode:

bool isSearching = false

when user types something:
  isSearching = true

when a category is clicked:
  isSearching = false

filtering:
  if isSearching then:
    filter based on query
  else:
    filter based on category

You can implement this logic in different events, such as EditText onQueryTextChange or Button onClickListener event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM