如何在嵌套的recyclerview中搜索内部项目?
我根据我的要求创建了一个嵌套的回收器视图,它工作正常。但现在我想搜索内部列表项(子回收器视图)。但它不能正常工作。
这是子适配器内自定义搜索过滤器的方法。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论