I have a action bar menu item with search action. I have a recyclerview which binds data from pagedlistadapter where it loads pagedlist. I need to filter the adapter data white the searchview text changes as we do in other adapters
I have done something like this, but it does not work.
Edits:
This is my complete code I use for adapter
public class ProductsRvAdapter extends PagedListAdapter<Product, ProductsRvAdapter.ViewHolder> implements Filterable {
private Context context;
public ProductsRvAdapter(Context context) {
super(DIFF_CALLBACK);
this.context = context;
}
private static DiffUtil.ItemCallback<Product> DIFF_CALLBACK = new DiffUtil.ItemCallback<Product>() {
@Override
public boolean areItemsTheSame(Product oldProduct, Product newProduct) {
return oldProduct.getId() == newProduct.getId();
}
@Override
public boolean areContentsTheSame(Product oldProduct, @NonNull Product newProduct) {
return oldProduct.equals(newProduct);
}
};
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_item_product, viewGroup, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
Product product = getItem(i);
if (product == null) {
viewHolder.clear();
} else {
viewHolder.bind(product);
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
//declare views
public ViewHolder(@NonNull View itemView) {
super(itemView);
//initialize views
}
public void bind(Product product) {
//bind data to views
}
public void clear() {
//clear views data
}
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String query = charSequence.toString();
PagedList<Product> filtered = null;
if (query.isEmpty()) {
filtered = getCurrentList();
} else {
for (Product product : itemList) {
if (product.getProductName().toLowerCase().contains(query.toLowerCase())) {
filtered.add(product);
}
}
}
FilterResults results = new FilterResults();
results.count = filtered.size();
results.values = filtered;
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults results) {
submitList((PagedList<Product>) results.values);
}
};
}
}
The thing is if you'll apply filter {} to PagedList you will have List and in
submitList((PagedList<Product>) results.values);
it will crash because types are different.
Behaviour what you want is possible but is different in case of "paged recycler"
In simple RecyclerView you load and "store" data in it so you can filter it. If user change page or scroll down you just add more data and again you can filter it (but you also have to fetch and add in adapter data from server with applied filter in case if they not exist in adapter yet).
In paged list, in my opinion, if you need to filter it, you should add filter in your DataSource and create new PagedList with this filtered data source (filter applied to api or database call in loadInitial and loadRange methods). So when it will scroll down (or load new page) you will load items from filtered pages.
Hope it'll help
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With