Set onItemClickListener inside onBindViewHolder() with RecyclerView.Adapter

后端 未结 5 1056
自闭症患者
自闭症患者 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:10

    replace this code,

            @Override public void onBindViewHolder (ViewHolder holder,int position){
    
                final Student student = students.get(position);
                holder.name.setText(student.getName());
                holder.age.setText(student.getAge());
    
    
                holder.v.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context, "CLICK : " + student.getName(), Toast.LENGTH_SHORT).show();
                    }
                });
    
                holder.v.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        Toast.makeText(context, "CLICK : " + student.getName(), Toast.LENGTH_SHORT).show();
                        return false;
                    }
                });
            }
    

提交回复
热议问题