如何搜索RecyclerView中显示的对象列表? [英] How to search a list of Objects that are displayed in a RecyclerView?

查看:222
本文介绍了如何搜索RecyclerView中显示的对象列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在尝试实现SearchView,但是显示的搜索结果仅对于第一个Search才更改,我输入到SearchView的其他所有内容都将被忽略.

I am currently trying to implement a SearchView, but the displayed search results change only for the first Search, everything else I enter into SearchView is ignored.

这是我的MainActivity.java中的搜索代码:

Here is my Code for Searching in my MainActivity.java:

testList = Arrays.asList(new TestItem("aw"), new TestItem("aa"), new TestItem("w"));

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.test_menu, menu);

    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setOnQueryTextListener(this);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onQueryTextChange(String newText) {
    List<TestItem> result = new ArrayList<>();
    for(TestItem t: testList) {
        if(t.getName().contains(newText) {
            result.add(t);
        }
    }
    adapter.changeList(result);
    return true;
}

adapter.changeList:

adapter.changeList:

public void changeList(List<TestItem> newList) {
    this.list = new ArrayList<>(newList);
}

每次更改文本后都会调用

onQueryTextChange,但是搜索结果仅显示一次.当输入aEnter时,RecyclerView显示awaa(是否必须按Enter键才能启动onQueryTextChange是正常的吗?),但是当我随后输入aa时,所显示的项目保持不变.一样.

onQueryTextChange gets called after each text change, but the search result is shown only once. When entering a and Enter the RecyclerView shows aw and aa (is it normal to have to press Enter for onQueryTextChange to start?), but when I then enter aa the displayed items stay the same.

推荐答案

我认为您的实现不正确.您无需按Enter键即可搜索结果.为了实现,您可以保留阵列列表的完整副本,然后制作另一个临时列表形式来更改查询文本.因此,在进行过滤时,您搜索的是实际列表,而不是过滤后的列表.

I think your implementation is not correct. You don't need to press the enter for search result. For implementation you can keep a intact copy of your array list.Then make another temporary list form that list when query text is changed. So when you are filtering, you are searching the actual list not the filtered list.

1.将搜索关键字传递到列表适配器

1.pass search keyword to the list adapter

public boolean onQueryTextChange(String newText) {

    adapter.getFilter().filter(newText);
    return true;
}

2.在适配器中实现Filterable和Override方法

2. In adapter implement Filterable and override method

@Override
public Filter getFilter() {
    if (frameFilter == null) {
        frameFilter = new FrameFilter();
    }

    return frameFilter;
}

3.过滤类可以是这样的:"categoryMain"是我的完整列表,"categoryTemp"是我的临时列表

3. Filtering class can be something like this: 'categoryMain' is my intact list and 'categoryTemp' is my temporary list

private class FrameFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        FilterResults filterResults = new FilterResults();
        if (constraint != null && constraint.length() > 0) {
            ArrayList<Category> tempList = new ArrayList<>();

            // search content in friend list
            Log.i("key",constraint.toString());
            for (Category category : categoriesMain) {
                if (category.getCategory_name().toLowerCase().contains(constraint.toString().toLowerCase())) {
                    tempList.add(category);
                }
            }
            filterResults.count = tempList.size();
            filterResults.values = tempList;
        } else {

            filterResults.count = categoriesMain.size();
            filterResults.values = categoriesMain.clone();
        }
        return filterResults;
    }

    /**
     * Notify about filtered list to ui
     *
     * @param constraint text
     * @param results    filtered result
     */
    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {

        categoriesTemp = (ArrayList<Category>) results.values;
        notifyDataSetChanged();
    }
}

这篇关于如何搜索RecyclerView中显示的对象列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆