title: Set onItemClickListener inside onBindViewHolder() with RecyclerView.Adapter
body: <p>I have a custom object :</p>
  
  <p>Student.class</p>
  
  <pre><code>public class Student {
    private String name;
    private String age;
  
    public String getName() {
      return name;
    }
  
    public void setName(String name) {
      this.name = name;
    }
  
    public String getAge() {
      return age;
    }
  
    public void setAge(String age) {
      this.age = age;
    }
  }
  </code></pre>
  
  <p>Then I implement <code>RecyclerView.Adapter</code> like this :</p>
  
  <p>MyAdapter.class</p>
  
  <pre><code>public class MyAdapter extends RecyclerView.Adapter&lt;MyAdapter.ViewHolder&gt; {
  
    private Context context;
    private ArrayList&lt;Student&gt; students = new ArrayList&lt;&gt;();
  
    public MyAdapter(Context mContext, ArrayList&lt;Student&gt; mStudents) {
      this.context = mContext;
      this.students = mStudents;
    }
  
    @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View v = LayoutInflater.from(context).inflate(R.layout.item_row, parent, false);
      return new ViewHolder(v);
    }
  
    @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.setClickListener(new ItemClickListener() {
        @Override public void onClickItem(int pos) {
          Toast.makeText(context, "CLICK : " + student.getName(), Toast.LENGTH_SHORT).show();
        }
  
        @Override public void onLongClickItem(int pos) {
          Toast.makeText(context, "LONG CLICK : " + student.getName(), Toast.LENGTH_SHORT).show();
        }
      });
    }
  
    @Override public int getItemCount() {
      return students.size();
    }
  
    public static class ViewHolder extends RecyclerView.ViewHolder
        implements View.OnClickListener, View.OnLongClickListener {
  
      private TextView name;
      private TextView age;
      private ItemClickListener mListener;
  
      public ViewHolder(View itemView) {
  
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.tv_name);
        age = (TextView) itemView.findViewById(R.id.tv_age);
  
        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);
      }
  
      public void setClickListener(ItemClickListener listener) {
        this.mListener = listener;
      }
  
      @Override public void onClick(View view) {
        mListener.onClickItem(getLayoutPosition());
      }
  
      @Override public boolean onLongClick(View view) {
        mListener.onLongClickItem(getLayoutPosition());
        return true;
      }
    }
  
    public interface ItemClickListener {
      void onClickItem(int pos);
  
      void onLongClickItem(int pos);
    }
  }
  </code></pre>
  
  <p>As you can see in <code>onBindViewHolder()</code>, I set <code>onItemClickListener()</code> for holder.</p>
  
  <pre><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.setClickListener(new ItemClickListener() {
        @Override public void onClickItem(int pos) {
          Toast.makeText(context, "CLICK : " + student.getName(), Toast.LENGTH_SHORT).show();
        }
  
        @Override public void onLongClickItem(int pos) {
          Toast.makeText(context, "LONG CLICK : " + student.getName(), Toast.LENGTH_SHORT).show();
        }
      });
    }
  </code></pre>
  
  <p>But I know <code>onBindViewHolder()</code> method will be called every time a new item scrolls into view. So call click listener inside <code>onBindViewHolder()</code> causes expensive operations inside it.</p>
  
  <p>How can I resolve this problem? </p>