从搜索视图的筛选器中选择文本时,在回收器视图中设置以前的结果



当从searchView的过滤器框中删除文本时,我在recyclerView中设置以前的结果时遇到问题。我在适配器中使用以下代码来精细过滤数据。但问题是,当从搜索视图中删除搜索文本时,它不会在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);

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

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

相关内容

  • 没有找到相关文章

最新更新