Set onItemClickListener inside onBindViewHolder() with RecyclerView.Adapter

后端 未结 5 1058
自闭症患者
自闭症患者 2021-02-14 03:54

I have a custom object :

Student.class

public class Student {
  private String name;
  private String age;

  public String getName() {
    return name;
         


        
5条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-14 03:54

    You need to set on onClickListener() on the view of the ViewHolder i.e. itemView in your case. It will call the onClick() method as soon as you click the complete view i.e. root view (itemView in your case)

    you can also set onClickListener() on the children of the root i.e. name and age.

    inside ViewHolder(View itemView) constructor:

    itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mContext,"clicked="+ getPosition(),Toast.LENGTH_SHORT).show();         
    
                    }
                });
    

    Similarly you can call onLongClickListner() also. And name.setOnClickLisner() on child view too.

提交回复
热议问题