Почему у cardView в recyclerView не срабатывает onClick?

вот MainActivity
RecyclerView recyclerView;
    RecyclerView.Adapter adapter;
    RecyclerView.LayoutManager layoutManager;
    ArrayList<RecipesItem> pizzaRecipesList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pizzaRecipesList = new ArrayList<>();
        pizzaRecipesList.add(new RecipesItem(R.drawable.eggrocketpizzas,"Title","description."));
        ...
        recyclerView = findViewById(R.id.pizzaList);
        recyclerView.setHasFixedSize(true);
        adapter = new PizzaAdapter(pizzaRecipesList, this);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(layoutManager);
    }

вот адаптер
public class PizzaAdapter  extends RecyclerView.Adapter<PizzaAdapter.PizzaHolder> {

    private final ArrayList<RecipesItem> recipesItemArrayList;
    private final Context context;

    public static class PizzaHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ImageView image;
        public TextView header;
        public TextView body;

        public PizzaHolder(@NonNull View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.image);
            header = itemView.findViewById(R.id.header);
            body = itemView.findViewById(R.id.body);
        }

        @Override
        public void onClick(View v) {
            Log.i("test",String.valueOf(getLayoutPosition()));
        }
    }

    public PizzaAdapter(ArrayList<RecipesItem> recipesItems, Context context) {
        this.recipesItemArrayList = recipesItems;
        this.context = context;
    }

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

    @Override
    public void onBindViewHolder(@NonNull PizzaHolder holder, int position) {
        RecipesItem recipesItem = recipesItemArrayList.get(position);
        holder.image.setImageResource(recipesItem.getImage());
        holder.header.setText(recipesItem.getTitle());
        holder.body.setText(recipesItem.getBody());
    }

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

при клике на любой элемент не приходит ни чего в лог. Можете подсказать почему?
  • Вопрос задан
  • 61 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы