java—如何在适配器中从另一个适配器触发器生成筛选器

lyr7nygr  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(283)

我有category recyclerview和product recyclerview,它们都有自己的适配器。类别回收器视图是用于过滤产品回收器视图的按钮。但我不知道如何从类别适配器的产品适配器过滤器。
这是我的类别适配器:

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder>{
    private final List<Category> listData;
    private int row_index = -1;

    public CategoryAdapter(List<Category> listData) {
        this.listData = listData;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        Category c =listData.get(position);
        holder.name.setText(c.getName());
        Glide.with(holder.itemView).load(c.getPicture_path()).into(holder.image);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 row_index = position;
                notifyDataSetChanged();
            }
        });

        if (row_index==position) {
            holder.button.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#0098FF")));
            holder.name.setTextColor(Color.parseColor("#FFFFFF"));

        } else {
            holder.button.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#FFFFFF")));
            holder.name.setTextColor(Color.parseColor("#9E9E9E"));
        }
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private final TextView name;
        private final ImageView image;
        private final LinearLayout button;
        public ViewHolder(View itemView) {
            super(itemView);
            name = (TextView)itemView.findViewById(R.id.text);
            image = (ImageView) itemView.findViewById(R.id.imageview);
            button = (LinearLayout) itemView.findViewById(R.id.button);
        }
    }
}

这是我的产品适配器

public class ProdukAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<Produk> produks;

    public ProdukAdapter(ArrayList<Produk> produks){
        this.produks = produks;
    }

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

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Produk produk = produks.get(position);

        TextView name = holder.itemView.findViewById(R.id.produk_name);
        TextView price = holder.itemView.findViewById(R.id.produk_price);
        ImageView images = holder.itemView.findViewById(R.id.produk_image);

        name.setText(produk.getName());
        price.setText(produk.getPrice_after());
        Glide.with(holder.itemView).load(produk.getPicture_path()).into(images);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(holder.itemView.getContext(), ProdukdetailActivity.class);
                intent.putExtra("name", produk.getName());
                intent.putExtra("price", produk.getPrice_after());
                intent.putExtra("image", produk.getPicture_path());
                holder.itemView.getContext().startActivity(intent);
            }
        });
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {
         public ViewHolder(View view){
             super(view);
         }
    }
}

因此,我需要在holder.itemview.setonclicklister从category adapter过滤来自产品适配器的数据,并将其显示给recyclerview。像“product.getname().等于”beef“”这样的筛选。
请帮我解决这个问题

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题