使用Recyclerview的searchview EditText

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Recyclerview的searchview EditText相关的知识,希望对你有一定的参考价值。

我想在我的recyclerview中添加搜索功能。我在google上找到了一个教程并实现了相同的功能。使用recyclerview中的editText进行搜索。但是当我输入输入时,我的列表消失了。我哪里错了?我的Recycler视图包含多个文本框。下面是MyAdapter class

如何使用以下代码实现搜索功能?

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>implements Filterable {
private List<ListItem> listItems;
private List<ListItem> mFilterData;
private Context context;
String m,e;
private ItemFilter mFilter = new ItemFilter();
public MyAdapter(List<ListItem> itemList, Context context) {
    this.listItems = itemList;
    this.context = context;
    this.mFilterData = listItems;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    ListItem listItem = mFilterData.get(position);
    holder.nameText.setText(listItem.getName());

    holder.addText.setText(listItem.getAddress());
    Picasso.with(context).load(listItem.getLogo()).resize(180,80).into(holder.imageText);

}

@Override
public int getItemCount() {
    return mFilterData.size();
}
public int getCount() {
    return mFilterData.size();
}
public ListItem getItem(int position) {
    return mFilterData.get(position);
}

public long getItemId(int position) {
    return position;
}

@Override
public Filter getFilter() {
    return mFilter;
}
private class ItemFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        String userString = constraint.toString().toLowerCase();
        FilterResults filterResults = new FilterResults();
        final List<ListItem> originalList = listItems;
        int count = originalList.size();
        final ArrayList<ListItem> resultList = new ArrayList<>(count);
        ListItem tDescription;
        for (int i = 0; i < count; i++) {
          tDescription= originalList.get(i);
            if (tDescription.getName().toLowerCase().contains(userString)) {
                resultList.add(tDescription);
            }
        }

        filterResults.values = resultList;
        filterResults.count = resultList.size();

        return filterResults;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        mFilterData = (ArrayList<ListItem>) results.values;
        notifyDataSetChanged();
   }
}

public class ViewHolder extends RecyclerView.ViewHolder{

    public TextView nameText, addText,mobText,emailText;
    public ImageView imageText;

    public ViewHolder(View itemView) {
        super(itemView);

   //code
    }
  }
}

搜索

 private void addTextListener() {
    search.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            s = s.toString().toLowerCase();

            final List<ListItem> filteredList = new ArrayList<>();

            for (int i = 0; i < listItems.size(); i++) {

                final String text = listItems.get(i).toString();
                if (text.contains(s)) {

                    filteredList.add(listItems.get(i));
                }
            }
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            adapter = new MyAdapter(filteredList, getActivity());
            recyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();  // data set changed

        }
    });
}
答案

试试下面的代码

private class ItemFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
     String userString = constraint.toString().toLowerCase();
     if (userString.isEmpty()) {
        mFilterData = listItems;
     } else {
         ArrayList<ListItem> resultList = new ArrayList<>();
         for (ListItem mListItem : listItems) {
             if (mListItem.getName().toLowerCase().contains(userString)) {
                   resultList.add(mListItem);
             }
         }

         mFilterData = resultList;
       }

         FilterResults filterResults = new FilterResults();
         filterResults.values = mFilterData;
         filterResults.count = mFilterData.size();
         return filterResults;
     }
}

更新

从edittext OnTextChanged中删除所有代码并编写下面的代码,无需设置适配器,只需在下面编写在线代码并删除其余代码

adapter.getFilter().filter(s.toString());

以上是关于使用Recyclerview的searchview EditText的主要内容,如果未能解决你的问题,请参考以下文章

当我们在 recyclerview 中使用 searchview 时,我们如何将搜索到的字符着色?

RecyclerView 的 SearchView 不起作用

使用 Room 和 RecyclerView 在 SearchView 中添加过滤器

如何在基于 SQLite 的 recyclerview 中实现 searchview? - 安卓

为啥 recyclerview 的 searchview 不起作用?

SearchView 在回收站视图中不起作用