如何在嵌套的recyclerview中搜索内部项目?

发布于 2025-01-12 04:18:19 字数 2770 浏览 0 评论 0原文

我根据我的要求创建了一个嵌套的回收器视图,它工作正常。但现在我想搜索内部列表项(子回收器视图)。但它不能正常工作。

这是子适配器内自定义搜索过滤器的方法。


    public class CustomSearchFilter extends Filter {
        CategoryItemsAdapter adapter;
        List<CategoryItemsModel> filterList;

        public CustomSearchFilter(List<CategoryItemsModel> filterList, CategoryItemsAdapter adapter) {
            this.adapter = adapter;
            this.filterList = filterList;
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if (constraint != null && constraint.length() > 0) {
                constraint = constraint.toString().toUpperCase();
                ArrayList<CategoryItemsModel> filteredPlayers = new ArrayList<>();
                for (int i = 0; i < filterList.size(); i++) {
                    if (filterList.get(i).getCategoryItemName().toLowerCase().contains(((String) constraint).toLowerCase())) {
                        filteredPlayers.add(filterList.get(i));
                    }
                }

                results.count = filteredPlayers.size();
                results.values = filteredPlayers;
            } else {
                results.count = filterList.size();
                results.values = filterList;

            }

            return results;
        }

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

    }

在类级别实现Filterable以覆盖下面的方法。

  @Override
    public Filter getFilter() {
        if (filter == null) {
            filter = new CustomSearchFilter(categoryItemArrayList, this);
        }
        return filter;
    }

OnActivityCreated 方法的片段中,我通过 editext 实现 addTextChangedListener,如下所示:

   edtSearchMenu.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable) {
                try {
                    text = edtSearchMenu.text.toString().toLowerCase()
                    innerItemsAdapter?.filter?.filter(text)
                } catch (e: Exception) {
                    Toast.makeText(activity, e.toString(), Toast.LENGTH_SHORT).show()
                }
            }
        })

How can I search inside items in Nested recycler view?

I've created a nested recycler view as per my requirement and it works fine. But now I want to search inner list items(child recycler view). But it doesn't work properly.

Here is the method for custom search filter inside child adapter.


    public class CustomSearchFilter extends Filter {
        CategoryItemsAdapter adapter;
        List<CategoryItemsModel> filterList;

        public CustomSearchFilter(List<CategoryItemsModel> filterList, CategoryItemsAdapter adapter) {
            this.adapter = adapter;
            this.filterList = filterList;
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if (constraint != null && constraint.length() > 0) {
                constraint = constraint.toString().toUpperCase();
                ArrayList<CategoryItemsModel> filteredPlayers = new ArrayList<>();
                for (int i = 0; i < filterList.size(); i++) {
                    if (filterList.get(i).getCategoryItemName().toLowerCase().contains(((String) constraint).toLowerCase())) {
                        filteredPlayers.add(filterList.get(i));
                    }
                }

                results.count = filteredPlayers.size();
                results.values = filteredPlayers;
            } else {
                results.count = filterList.size();
                results.values = filterList;

            }

            return results;
        }

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

    }

Implement Filterable on the class level to override method below.

  @Override
    public Filter getFilter() {
        if (filter == null) {
            filter = new CustomSearchFilter(categoryItemArrayList, this);
        }
        return filter;
    }

In my fragment in the OnActivityCreated method, I implement addTextChangedListener via editext like this:

   edtSearchMenu.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable) {
                try {
                    text = edtSearchMenu.text.toString().toLowerCase()
                    innerItemsAdapter?.filter?.filter(text)
                } catch (e: Exception) {
                    Toast.makeText(activity, e.toString(), Toast.LENGTH_SHORT).show()
                }
            }
        })

How can I search inner items in nested recycler view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文