android – 使用Searchview通过RecyclerView搜索

前端之家收集整理的这篇文章主要介绍了android – 使用Searchview通过RecyclerView搜索前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想通过RecyclerView搜索,我有List< BaSEOfCards> (BaSEOfCards是我的getter& setter类)
我的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中初始化它:

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

接下来我需要做什么?让我的RecyclerViewAdapter实现Filterable或者什么?或者只是在* RecyclerViewAdapter **中进行类过滤,然后从我的MainActivity中调用它?

解决方法

解决了我的问题

>让我的类RecyclerViewAdapter实现Filterable
>添加行私有列表< BaSEOfCards>原稿;
>在RecyclerViewAdapter中添加方法getFilter

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并更改onTextTextChange方法

@Override
public boolean onQueryTextChange(String newText) {
    if ( TextUtils.isEmpty ( newText ) ) {
        adapter.getFilter().filter("");
    } else {
        adapter.getFilter().filter(newText.toString());
    }
    return true;
}
原文链接:https://www.f2er.com/android/316686.html

猜你在找的Android相关文章