Set onItemClickListener inside onBindViewHolder() with RecyclerView.Adapter

后端 未结 5 1055
自闭症患者
自闭症患者 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 04:01

    You can implement onClickListener for the parent view like below:

    //holder.view - parent layout inside recyclerView item
    holder.view.setTag(position);
    holder.view..setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = (int) v.getTag();
                Student student = students.get(pos);
                // Do your operation
    
            }
        });
    

    OR

    if you want to set onItemClickListener from activity/fragment where recyclerView is initialised, you can follow this link: http://www.littlerobots.nl/blog/Handle-Android-RecyclerView-Clicks/

提交回复
热议问题