java - 如何将搜索过滤器应用到带有分类过滤的回收 View ?

标签 java android

现在,我有一个回收 View ,其中包含两个类别的项目,以及 Activity 上的三个按钮,这些按钮按“全部”、“设计类别”和“开发类别”过滤它们。单击按钮时,仅显示该类别的项目。我还有一个搜索栏,可以按名称过滤结果。在搜索栏中输入内容并过滤结果后,如果单击分类按钮,它将显示该类别的所有项目,而不是仅显示与搜索名称匹配的项目。

我当前唯一的解决方案是在单击按钮时清除搜索栏,但我想这样做,以便在单击按钮后搜索栏过滤器仍然适用。

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);
    }

最佳答案

本质上,您的用户在显示列表之前有两种不同的方法来过滤列表。一方面,他们可以使用按钮选择类别;或者他们可以输入一些文本并通过搜索进行过滤。

现在的主要问题是辨别两种相互竞争的过滤方法。如果用户既单击了类别又输入了查询,我们不知道应该遵循哪种过滤方法。

在我看来,我们应该始终尊重最新的行动。

解决此问题的一种方法是,例如,使用一个变量来记录用户的最新操作。伪代码示例:

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

您可以在不同的事件中实现此逻辑,例如 EditText onQueryTextChange 或 Button onClickListener 事件。

关于java - 如何将搜索过滤器应用到带有分类过滤的回收 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57483965/

相关文章:

android - 在 Android 的锁定屏幕上显示警告对话框

java - 将 Activity 复制到 fragment

java - jni 代码中的 FindClass 使 JVM 崩溃

java - zxing - 生成自定义二维码模板

java - 接收 SSLHandshakeException : handshake_failure despite my client ignoring all certs

java - Spring 启动+Maven : repositoryFactoryBean not found

java - 在java中调用许多类 - android studio

android - 选项卡、 Material 设计和支持库

java - 如何只从列表中删除一个重复项?

java - logback 模式在消息后打印 [jar]