从“搜索”视图的“过滤器”中选择文本时,将以前的结果设置回回收器视图中

时间:2018-03-22 09:46:57

标签: android android-recyclerview filtering searchview recycler-adapter

recyclerView的过滤框中删除文字后,我在searchView中设置以前的结果时遇到问题。我在适配器中使用以下代码来精确地过滤数据。但问题是,当从搜索视图中删除搜索文本时,它不会在recyclerView中设置先前的值。

recyclerView适配器的获取过滤方法

@Override
    public  Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String charString = charSequence.toString();
                if (charString.isEmpty()) {
                    results = names;
                } else {
                    ArrayList<tab> filteredList = new ArrayList<>();
                    for (tab row : names) {

                        // name match condition. this might differ depending on your requirement
                        // here we are looking for name or phone number match
                        if (row.getName().toLowerCase().contains(charString.toLowerCase()) || row.getmsg().contains(charSequence)) {
                            filteredList.add(row);
                        }
                    }

                    results = filteredList;
                }

                FilterResults filterResults = new FilterResults();
                filterResults.values = results;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                names = (ArrayList<tab>) filterResults.values;

                // refresh the list with filtered data
                notifyDataSetChanged();
            }
        };
    }

主要活动代码

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        // Associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView = (SearchView) menu.findItem(R.id.action_search)
                .getActionView();
        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getComponentName()));
        searchView.setMaxWidth(Integer.MAX_VALUE);

        // listening to search query text change
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                // filter recycler view when query submitted
                gtxx.getFilter().filter(query);
               // gtxx.notifyDataSetChanged();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String query) {
                // filter recycler view when text is changed
               gtxx.getFilter().filter(query);
               //gtxx.notifyDataSetChanged();
                return false;
            }
        });
        return true;

    }

在适配器中设置值

 gtxx = new listadapttor(con_list);

            recyclerView.setAdapter(gtxx);

1 个答案:

答案 0 :(得分:1)

在适配器中创建两个具有相同数据的列表。让我们说第二个列表是临时列表。使用该列表设置适配器并对第一个列表中的第二个列表进行过滤。请参考以下代码:

private List<YourObject> tempList;

@Override
public Filter getFilter() {
    Filter filter = new Filter() {

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            //this is the main list, which contains filtered items
            //and tempList always contains original data.
            //so when empty text is passed to filterable it will reset
            //the list using original data from tempList
            mainList = (List<YourObject>) results.values;
            notifyDataSetChanged();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults results = new FilterResults();

            if (constraint != null && constraint.toString().length() > 0) {
                List<YourObject> filteredItems = new ArrayList<YourObject>();
                for (int i = 0; i < tempList.size(); i++) {
                    YourObject data = tempList.get(i);
                    if ((data.getName() != null && data.getName().toLowerCase().contains(constraint.toString().toLowerCase()))) { //Your filtering code here
                        filteredItems.add(data);
                    }
                }

                results.count = filteredItems.size();
                results.values = filteredItems;

            }
            else
            {
                synchronized (this) {
                    results.values = tempList;
                    results.count = tempList.size();
                }
            }
            return results;
        }
    };

    return filter;
}