本文介绍了使用 Searchview 搜索 RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过RecyclerView进行搜索,我有List(BaseOfCards是我的getter&setter类)我的 RecyclerViewAdapter :

I want to search through RecyclerView, I have List<BaseOfCards> (BaseOfCards is my getter&setter class)My RecyclerViewAdapter :

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {


private LayoutInflater inflater;
private List<BaseOfCards> items;

//private int itemLayout;
//String cardvalue;
private Activity mActivity;

public RecyclerViewAdapter(Activity mActivity, Context context, List<BaseOfCards> items) {
    this.mActivity = mActivity;
    inflater = LayoutInflater.from(context);
    this.items = items;
    //this.itemLayout = itemLayout;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = inflater.inflate(R.layout.custom_row, parent, false);
    MyViewHolder holder = new MyViewHolder(view, mActivity);
    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

    BaseOfCards item = items.get(position);
    holder.title.setTag(item);
    holder.title.setText(item.getCardName());
}

@Override
public int getItemCount() {

    return items.size();
}

public static class MyViewHolder extends RecyclerView.ViewHolder {
    private Activity mActivity;
    TextView title;
    ImageView titileImageView;

    public MyViewHolder(View itemView, Activity mActivity) {

        super(itemView);

        titileImageView = (ImageView) itemView.findViewById(R.id.image_country);


        title = (TextView) itemView.findViewById(R.id.listText);
        this.mActivity = mActivity;

    }
}

}

我将 SearchView 添加到我的菜单并在 MainActivity 中初始化它:

I add SearchView to my menu and initialize it in MainActivity:

 MenuItem menuItem = menu.findItem(R.id.action_search1);
    searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
    SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView.setIconifiedByDefault(true);

接下来我需要做什么?让我的 RecyclerViewAdapter implement Filterable 还是什么?或者只是在 *RecyclerViewAdapter** 中创建类 filter 并从我的 MainActivity 调用它?

What do I need to do next? Make my RecyclerViewAdapter implement Filterable or what? Or just make class filter in *RecyclerViewAdapter** and just call it from my MainActivity ?

推荐答案

我解决了我的问题

  1. 使我的类 RecyclerViewAdapter 实现可过滤

添加行 private List原件;

RecyclerViewAdapter

public Filter getFilter() {
return new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        final FilterResults oReturn = new FilterResults();
        final List<BaseOfCards> results = new ArrayList<BaseOfCards>();
        if (orig == null)
            orig  = items;
            if (constraint != null){
                if(orig !=null & orig.size()>0 ){
                    for ( final BaseOfCards g :orig) {
                        if (g.getCardName().toLowerCase().contains(constraint.toString()))results.add(g);
                    }
                }
                oReturn.values = results;
            }
            return oReturn;
        }

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

    }
};

  • 制作 MainActivity 实现 SearchView.OnQueryTextListener 并更改方法 onQueryTextChange :

    @Override
    public boolean onQueryTextChange(String newText) {
        if ( TextUtils.isEmpty ( newText ) ) {
            adapter.getFilter().filter("");
        } else {
            adapter.getFilter().filter(newText.toString());
        }
        return true;
    }
    

  • 这篇关于使用 Searchview 搜索 RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    06-18 10:06