当ViewHolder和Adapter处于单独的类中时,可过滤的Searchview

时间:2019-08-30 21:51:07

标签: android searchview

我已经创建了可过滤的搜索视图,并且希望在过滤后获得正确的点击项。

列表示例

  • 苹果
  • 鳄梨
  • 香蕉

过滤列表示例

  • 香蕉

如果在过滤后点击Banana,我将收到Apple的结果。

我已经为ViewHolder和Adapter分离了文件。

ViewHolder

public class ProductHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private OnProductClickListener onProductClickListener;

    @BindView(R.id.ivProduct) public ImageView productImage;
    @BindView(R.id.tvProductTitle) public TextView productTitle;
    @BindView(R.id.tvProductDesc) public TextView productDesc;

    public ProductHolder(View itemView, final ProductHolder.OnProductClickListener onProductClickListener) {
        super(itemView);

        ButterKnife.bind(this, itemView);

        this.onProductClickListener = onProductClickListener;
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        onProductClickListener.onProductClick(getAdapterPosition());
    }

    public interface OnProductClickListener {
        void onProductClick(int position);
    }
}

适配器

    public class ProductAdapter extends RecyclerView.Adapter<ProductHolder> implements Filterable {

    private static final String TAG = "ProductAdapter";

    private Context mContext;
    private List<Product> productList;
    private List<Product> productListFiltered;
    private ProductHolder.OnProductClickListener onProductClickListener;

    public ProductAdapter(Context mContext, List<Product> productList, ProductHolder.OnProductClickListener onProductClickListener) {
        this.mContext = mContext;
        this.productList = productList;
        this.productListFiltered = productList;
        this.onProductClickListener = onProductClickListener;
    }

    @NonNull
    @Override
    public ProductHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_products, parent, false);
        return new ProductHolder(view, onProductClickListener);
    }

    @Override
    public void onBindViewHolder(@NonNull ProductHolder holder, int position) {
//        Product product = productList.get(position);
        Product product = productListFiltered.get(position);

//        holder.productImage
        holder.productTitle.setText(product.getDescricao());
        holder.productDesc.setText(product.getCodigo());

    }

    @Override
    public int getItemCount() {
//        return productList != null ? productList.size() : 0;
        Log.d(TAG, "getItemCount: " + productListFiltered.size());
        return productListFiltered != null ? productListFiltered.size() : 0;
    }

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

                String charString = charSequence.toString();

                if (charString.isEmpty()) {
                    productListFiltered = productList;
                } else {
                    List<Product> filteredList = new ArrayList<>();
                    for (Product row : productList) {

                        // here we are looking for descricao or codigo number match
                        if (row.getDescricao().toLowerCase().contains(charString.toLowerCase()) || row.getCodigo().contains(charSequence)) {
                            Log.d(TAG, "performFiltering: " + row.getDescricao());
                            filteredList.add(row);
                        }
                    }
                    productListFiltered = filteredList;
                }

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

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                productListFiltered = (ArrayList<Product>) filterResults.values;
                Log.d(TAG, "publishResults: " + productListFiltered.get(0).getDescricao());
                notifyDataSetChanged();
            }
        };
    }
}

我可能需要将ProductHolder中的onClick方法更改为类似的

onProductClickListener.onProductClick(productListFiltered.get(getAdapterPosition()));

但是我真的不知道该怎么实现。

注意:我知道我可以通过一个单一的类来实现。

0 个答案:

没有答案