title: RecyclerView onItemClickListener
body: <p>I'm looking to build a program where someone will click from a list of text and this will take them to another activity.</p>
  
  <p>I'm aware that RecyclerView doesn't have a listener for clicks, like ListView but, after several attempts, I am unable to get this to work.</p>
  
  <p>Below is the code I have attempted to implement:</p>
  
  <p>MyChatRecyclerViewAdapter.java</p>
  
  <pre><code>public class MyChatRecyclerViewAdapter extends RecyclerView.Adapter&lt;MyChatRecyclerViewAdapter.ViewHolder&gt; {
  
      private final List&lt;Chat&gt; mValues;
      private final OnItemClickListener listener;
      //private final OnListFragmentInteractionListener mListener;
  
      public MyChatRecyclerViewAdapter(List&lt;Chat&gt; items, OnItemClickListener listener) {
          this.mValues = items;
          //mListener = listener;
          this.listener = listener;
      }
  
      @Override
      public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View view = LayoutInflater.from(parent.getContext())
                  .inflate(R.layout.fragment_chat, parent, false);
          return new ViewHolder(view);
      }
  
      @Override
      public void onBindViewHolder(ViewHolder holder, int position) {
          holder.mItem = mValues.get(position);
          holder.mIdView.setText(mValues.get(position).id);
          holder.bind(mValues.get(position), listener);
          holder.mContentView.setText(mValues.get(position).content);
  
          holder.mView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (null != listener) {
                      // Notify the active callbacks interface (the activity, if the
                      // fragment is attached to one) that an item has been selected.
                      listener.onListFragmentInteraction(holder.mItem);
                  }
              }
          });
      }
  
      @Override
      public int getItemCount() {
          return mValues.size();
      }
  
  
      public interface OnItemClickListener {
          //public void onItemClick(View view , int position);
          void onItemClick(Chat item);
      }
  
  
      public class ViewHolder extends RecyclerView.ViewHolder{
          public final View mView;
          public final TextView mIdView;
          public final TextView mContentView;
          public Chat mItem;
          OnItemClickListener mItemClickListener;
  
          /*@Override
          public void onClick(View v) {
              if (mItemClickListener != null) {
                  mItemClickListener.onItemClick(v, getPosition());
              }
          }*/
  
          public ViewHolder(View itemView) {
              super(itemView);
              mView = itemView;
              mIdView = (TextView) itemView.findViewById(R.id.id);
              mContentView = (TextView) itemView.findViewById(R.id.content);
          }
  
          public void bind(final Chat item, final OnItemClickListener listener){
  
              itemView.setOnClickListener(new View.OnClickListener(){
                  @override
                  public void onClick(View v){
                      listener.onItemClick(item);
                  }
              });
          }
  
          @Override
          public String toString() {
              return super.toString() + " '" + mContentView.getText() + "'";
          }
      }
  }
  </code></pre>
  
  <p>This is the tutorial I have attempted to follow: <a href="https://antonioleiva.com/recyclerview-listener/" rel="nofollow noreferrer">https://antonioleiva.com/recyclerview-listener/</a></p>