recyclerview搜索过滤器不起作用?

时间:2018-06-19 10:07:49

标签: android search filter android-recyclerview

我在适配器类中编写了getFilter(),在主要活动中编写了setOnQueryTextListener但是不知道它不是过滤也不是搜索的问题请帮助我需要实现搜索过滤器Recycler查看列表必须在输入时进行过滤。

//适配器类

public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.MenuViewHolder> implements Filterable {

    private Context mCtx;
    private List<Menus> menusList;
    private List<Menus> menusFilteredList;

    public MenuAdapter(Context mCtx, List<Menus> menusList) {
        this.mCtx = mCtx;
        this.menusList = menusList;
    }


    @Override
    public MenuAdapter.MenuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.menu_list, null);
        return new MenuAdapter.MenuViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MenuAdapter.MenuViewHolder holder, final int position) {
        final Menus menus = menusList.get(position);

        //loading the image
        Glide.with(mCtx)
                .load(menus.getMenuimage())
                .into(holder.menuimage);


        holder.menuname.setText(menus.getMenuname());
        holder.cardview.setCardBackgroundColor(Color.parseColor(menus.getMenubg()));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Toast.makeText(mCtx, "" + menus.getMenuname(), Toast.LENGTH_SHORT).show();


                if (menus.getMenuurl().getBytes().length == 0)
                {

                    Toast.makeText(mCtx, "Not Available.", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Intent web = new Intent(mCtx,WebActivity.class);
                    web.putExtra("url",menus.getMenuurl());
                    web.putExtra("urltitle",menus.getMenuname());
                    mCtx.startActivity(web);
                    ((Activity)mCtx).finish();

                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return menusFilteredList.size();
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {

                String charString = charSequence.toString();

                if (charString.isEmpty()) {

                    menusFilteredList = menusList;

                } else {

                   // List<Menus> filteredList = new List<>();
                    List<Menus> filteredList = new ArrayList<>();
                    for (Menus menus : menusList) {

                        if (menus.getMenuname().toLowerCase().contains(charString) || menus.getMenuurl().toLowerCase().contains(charString)) {

                            filteredList.add(menus);
                        }
                    }

                    menusFilteredList = filteredList;
                }

                FilterResults filterResults = new FilterResults();
                filterResults.values = menusFilteredList;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                menusList = (ArrayList<Menus>) filterResults.values;
                notifyDataSetChanged();
            }
        };
    }

    class MenuViewHolder extends RecyclerView.ViewHolder {
        ImageView menuimage;
        TextView menuname;
        CardView cardview;

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


            menuimage = itemView.findViewById(R.id.menuimage);
            menuname = itemView.findViewById(R.id.menuname);
            cardview  = itemView.findViewById(R.id.cardview);

        }

    }
}

// mainactivity

  @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);

    MenuItem search = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(search);
    search(searchView);
    return true;

}

private void search(SearchView searchView) {

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            mAdapter.getFilter().filter(newText);
            return true;
        }
    });
}

1 个答案:

答案 0 :(得分:0)

你想设置计数值

 @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        filteredList.clear();
        final FilterResults results = new FilterResults();
        if (constraint.length() == 0) {
            filteredList.addAll(menusList);
        } else {
            final String filterPattern = constraint.toString().toLowerCase().trim();

           for (Menus menus : menusList) {

                    if (menus.getMenuname().toLowerCase().contains(charString) || menus.getMenuurl().toLowerCase().contains(charString)) {

                        filteredList.add(menus);
                    }
                }
        }
        System.out.println("Count Number " + filteredList.size());
        results.values = filteredList;
        results.count = filteredList.size();
        return results;
    }

请注意:过滤列表应仅在适配器

的初始阶段加载所有列表项