欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android RecyclerView添加搜索过滤器的示例代码

时间:2021-01-31 08:07:56|栏目:Android代码|点击:

搜索过滤功能,相信大家都能用到,一般都是针对列表进行过滤的。下面给大家提供一种过滤列表的方法。

老规矩,先上图

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 {
      List<String> 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<String>) filterResults.values;
     //刷新数据
     notifyDataSetChanged();
    }
   };
  }

完整Adapter代码:

//这里实现Filterable接口
class TitleAdapter extends RecyclerView.Adapter<TitleAdapter.TitleHolder> implements Filterable {

  private List<String> mSourceList = new ArrayList<>();
  private List<String> mFilterList = new ArrayList<>();

  private void appendList(List<String> 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<String> 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<String>) 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 List<String> mList = 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自定义Dialog弹框和背景阴影显示效果

栏    目:Android代码

下一篇:android通过servlet上传文件到服务器

本文标题:Android RecyclerView添加搜索过滤器的示例代码

本文地址:http://www.codeinn.net/misctech/54164.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有