title: OnClick for item in RecyclerView
body: <p>I am witnessing a wierd behavior of <code>RecyclerView</code>. so I have set an OnClickListener in the <code>ViewHolder</code> on a <code>Button</code> which is meant to do 3 things, when the user clicks:</p>
  
  <ol>
  <li><p>Add the current item's data to an <code>ArrayList</code> [displayed on a different <code>Fragment</code>]</p></li>
  <li><p>Display a <code>Toast</code> to the user.</p></li>
  <li>modify the UI of the selected item. [i.e setVisibility of Button to <code>View.INVISIBLE</code> etc..]</li>
  </ol>
  
  <p>now, while the first two tasks of the <code>OnClickListener</code> are fulfilled successfully and apply <strong>only</strong> to the selected item, the third one which is meant to modify the UI of the <strong>selected</strong> item only, instead gets also applied to each 9th Item on the <code>RecyclerView</code>.  </p>
  
  <ol>
  <li>How do I  prevent this behavior?</li>
  <li>Why is this behavior occurring with the 3rd task only?</li>
  </ol>
  
  <p><strong>i.e</strong> from previous answers on this. particular topic that I saw, I got the notion that it's better to insert the <code>OnClickListener</code> in the <code>ViewHolder</code> rather then putting it in <code>onBindViewHolder</code>, which did not solve the issue. I have followed other suggestions on stackOverflow and all failed.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>static class ItemsRecyclerAdapter extends RecyclerView.Adapter&lt;ItemsRecyclerAdapter.ItemsViewHolder&gt;{
  
      FragmentActivity fragmentActivity;
      List&lt;ProductItem&gt; data;
      LayoutInflater inflater;
      private int id;
      private int shopCode;
  
      public ItemsRecyclerAdapter(List&lt;ProductItem&gt; data, FragmentActivity fragmentActivity, int id, int shopCode) {
          this.fragmentActivity = fragmentActivity;
          this.data = data;
          this.inflater = LayoutInflater.from(fragmentActivity);
          this.id = id;
          this.shopCode = shopCode;
      }
  
      @Override
      public ItemsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View v = inflater.inflate(R.layout.pricing_item, parent, false);
  
          return new ItemsViewHolder(v);
      }
  
      @Override
      public void onBindViewHolder(final ItemsViewHolder holder, int position) {
          ProductItem itemPrice = data.get(position);
          holder.productName.setText(itemPrice.getProductName());
          holder.quantity.setText(itemPrice.getProductQuantity());
          holder.tvPrice.setText(itemPrice.getproductPrice());
          Picasso.with(fragmentActivity).load(itemPrice.getThumbUrl()).into(holder.imgProduct);
  
      }
  
      @Override
      public int getItemCount() {
          return data.size();
      }
  
  
          class ItemsViewHolder extends RecyclerView.ViewHolder{
  
          TextView productName, quantity, tvPrice;
          ImageView imgProduct;
          Button btnAddToBasket, btnAdd, btnRemove;
  
          Typeface myCustomFont = Typeface.createFromAsset(fragmentActivity.getAssets(), "fonts/Assistant-SemiBold.ttf");
  
          public ItemsViewHolder(View itemView) {
              super(itemView);
              productName = (TextView) itemView.findViewById(R.id.productName);
              quantity = (TextView) itemView.findViewById(R.id.manufacturer);
              tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
              imgProduct = (ImageView) itemView.findViewById(R.id.imgProduct);
              btnAddToBasket = (Button) itemView.findViewById(R.id.btnAddToBasket);
              btnAdd = itemView.findViewById(R.id.btnAdd);
              btnRemove = itemView.findViewById(R.id.btnRemove);
  
              btnAdd.setVisibility(View.INVISIBLE);
              btnRemove.setVisibility(View.INVISIBLE);
  
              quantity.setTypeface(myCustomFont);
              productName.setTypeface(myCustomFont);
              tvPrice.setTypeface(myCustomFont);
  
              Typeface fontAwesomeBasketIcon = Typeface.createFromAsset(fragmentActivity.getAssets(), "fontawesome-webfont.ttf");
              btnAddToBasket.setTypeface(fontAwesomeBasketIcon);
              btnAdd.setTypeface(fontAwesomeBasketIcon);
              btnRemove.setTypeface(fontAwesomeBasketIcon);
  
  
  
              btnAddToBasket.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      int pos = getAdapterPosition();
  
                      MyBasketFragment.myBasketList
                              .add(new ProductItem(data.get(pos).getProductName(), data.get(pos).getProductQuantity(),
                                      data.get(pos).getproductPrice(), data.get(pos).getThumbUrl(), id, shopCode));
  
                      btnAddToBasket.setOnClickListener(null);
  
  
                      Toast.makeText(fragmentActivity, "New product..", Toast.LENGTH_SHORT).show();
  
  
  
                      btnAdd.setVisibility(View.VISIBLE);
                      btnRemove.setVisibility(View.VISIBLE);
  
  
                  }
              });
  
              btnAdd.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      ItemsFragment.atomic.incrementAndGet();
                      btnAddToBasket.setText(String.valueOf(atomic.get()));
                  }
              });
          }
      }
  
    }
  </code></pre>