Why does OnClickListener on a ViewHolder don't work?

前端 未结 3 1530
孤街浪徒
孤街浪徒 2020-11-30 06:36

I\'m trying to implement a way to handle item selection on a RecyclerView. I personally don\'t like the way suggested in some answers on SO of passing through g

3条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 07:11

    Looking at the updated code: you are not setting the onClickListener to any of the views in the ViewHolder. It is an understandable mistake to forget the click listener.

    Just use:

    tvName.setOnClickListener(this);
    star.setOnClickListener(this);
    

    You can set to both or just one of them. You can also simply get the parent layout of these two views, so that the whole item itself in the adapter can be clickable.

    itemView.setOnClickListener(this);
    

提交回复
热议问题