title: How do I get the position selected in a RecyclerView?
body: <p>I am experimenting with the support library's recyclerview and cards.  I have a recyclerview of cards.  Each card has an 'x' icon at the top right corner to remove it:</p>
  
  <p>The card xml, <code>list_item.xml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.v7.widget.CardView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_margin="5dp"&gt;
  &lt;RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
      &lt;TextView
          android:id="@+id/taskDesc"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:textSize="40sp"
          android:text="hi"/&gt;
      &lt;ImageView
          android:id="@+id/xImg"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          android:layout_alignParentRight="true"
          android:src="@drawable/ic_remove"/&gt;
  &lt;/RelativeLayout&gt;
  &lt;/android.support.v7.widget.CardView&gt;
  </code></pre>
  
  <p>I attempted to tag the row with the position I would use in <code>notifyItemRemoved(position)</code> in <code>TaskAdapter.java</code>:</p>
  
  <pre><code>public class TaskAdapter extends RecyclerView.Adapter&lt;TaskAdapter.TaskViewHolder&gt;  {
  
  private List&lt;Task&gt; taskList;
  private TaskAdapter thisAdapter = this;
  
  
  // cache of views to reduce number of findViewById calls
  public static class TaskViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
      protected TextView taskTV;
      protected ImageView closeBtn;
      public TaskViewHolder(View v) {
          super(v);
          taskTV = (TextView)v.findViewById(R.id.taskDesc);
      }
  
      @Override
      public void onClick(View v) {
          int position = v.getTag();
          adapter.notifyItemRemoved(position);
      }
  }
  
  
  public TaskAdapter(List&lt;Task&gt; tasks) {
      if(tasks == null)
          throw new IllegalArgumentException("tasks cannot be null");
      taskList = tasks;
  }
  
  
  // onBindViewHolder binds a model to a viewholder
  @Override
  public void onBindViewHolder(TaskViewHolder taskViewHolder, int pos) {
      final int position = pos;
      Task currTask = taskList.get(pos);
      taskViewHolder.taskTV.setText(currTask.getDescription());
  
      taskViewHolder.closeBtn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              thisAdapter.notifyItemRemoved(position);
          }
      });
  }
  
  @Override
  public int getItemCount() {
      return taskList.size();
  }
  
  
  // inflates row to create a viewHolder
  @Override
  public TaskViewHolder onCreateViewHolder(ViewGroup parent, int pos) {
      View itemView = LayoutInflater.from(parent.getContext()).
                                     inflate(R.layout.list_item, parent, false);
  
      return new TaskViewHolder(itemView);
  }
  }
  </code></pre>
  
  <p>This won't work because you can't set a tag nor can I access the adapter from <code>onClick.</code></p>