十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本篇文章为大家展示了怎么在Android中使用RecyclerView添加搜索过滤器,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
公司主营业务:网站建设、成都做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联建站是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联建站推出梅州免费做网站回馈大家。
RecycleView搜索过滤器-getFilter()
Android 提供了Fileterable类,可以通过过滤器(条件)来过滤数据。通常,getFilter()方法必须在提供过滤条件的适配器类中被重写,以通过列表进行搜索。下面是通过getFilter(),来过滤数据。
@Override public Filter getFilter() { return new Filter() { @Override protected FilterResults performFiltering(CharSequence charSequence) { String charString = charSequence.toString(); if (charString.isEmpty()) { mFilterList = mSourceList; } else { ListfilteredList = new ArrayList<>(); for (String str : mSourceList) { //这里根据需求,添加匹配规则 if (str.contains(charString)) { filteredList.add(str); } } mFilterList = filteredList; } FilterResults filterResults = new FilterResults(); filterResults.values = mFilterList; return filterResults; } @Override protected void publishResults(CharSequence charSequence, FilterResults filterResults) { mFilterList = (ArrayList ) filterResults.values; //刷新数据 notifyDataSetChanged(); } }; }
完整Adapter代码:
//这里实现Filterable接口 class TitleAdapter extends RecyclerView.Adapterimplements Filterable { private List mSourceList = new ArrayList<>(); private List mFilterList = new ArrayList<>(); private void appendList(List list) { mSourceList = list; //这里需要初始化filterList mFilterList = list; } @Override public TitleHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new TitleHolder(LayoutInflater.from(FastScrollRecyclerActivity.this).inflate(R.layout.item_textview_view, parent, false)); } @Override public void onBindViewHolder(TitleHolder holder, int position) { //这里也是过滤后的list holder.tv.setText(mFilterList.get(position)); } @Override public int getItemCount() { //注意这里需要是过滤后的list return mFilterList.size(); } //重写getFilter()方法 @Override public Filter getFilter() { return new Filter() { //执行过滤操作 @Override protected FilterResults performFiltering(CharSequence charSequence) { String charString = charSequence.toString(); if (charString.isEmpty()) { //没有过滤的内容,则使用源数据 mFilterList = mSourceList; } else { List filteredList = new ArrayList<>(); for (String str : mSourceList) { //这里根据需求,添加匹配规则 if (str.contains(charString)) { filteredList.add(str); } } mFilterList = filteredList; } FilterResults filterResults = new FilterResults(); filterResults.values = mFilterList; return filterResults; } //把过滤后的值返回出来 @Override protected void publishResults(CharSequence charSequence, FilterResults filterResults) { mFilterList = (ArrayList ) filterResults.values; notifyDataSetChanged(); } }; } class TitleHolder extends RecyclerView.ViewHolder { TextView tv; public TitleHolder(View itemView) { super(itemView); tv = (TextView) itemView.findViewById(R.id.id_tv_test); } } }
数据初始化部分
private String[] str = new String[]{ "apple", "apple juice", "apple pie", "abalone", "bread", "brandy", "Blueberry", "Banana", "chocolate", "cake", "chicken", "cheese", "Durian", "Dim Sam", "Dumpling", "duck", "egg", "English muffin", "eggplant", "French toast", "fish", "fig", "fruit" };
private ListmList = new ArrayList<>(); private void initList() { for (int i = 0; i < str.length; i++) { mList.add(str[i]); } }
设置RecycleView和EditText监听
RecyclerView rv = (RecyclerView) findViewById(R.id.id_rv); LinearLayoutManager manager = new LinearLayoutManager(this); rv.setLayoutManager(manager); final TitleAdapter adapter = new TitleAdapter(); adapter.appendList(mList); rv.setAdapter(adapter); EditText et = findViewById(R.id.id_et); et.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence sequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence sequence, int i, int i1, int i2) { adapter.getFilter().filter(sequence.toString()); } @Override public void afterTextChanged(Editable editable) { } });
上述内容就是怎么在Android中使用RecyclerView添加搜索过滤器,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。