title: RecyclerView items not repeating, but retaining changes of previous item that was in its slot
body: <p>I am trying to implement a list of files that can be selected from the RecyclerView Adapter class. While I understand it is not a good idea, I feel if I am able to accomplish this from within said class, it would be really helpful in the future.
  My list item (Each individual item view for the RecyclerView) has the following structure:</p>
  <pre><code>|--------|----------------|
  |  ICON  |      DATA      |
  |--------|----------------|
  </code></pre>
  <ul>
  <li><p>Problem:</p>
  <ul>
  <li><p>When a file is selected (by touching the icon portion of a file item), I change the background of that item to another color to denote that it has been selected.</p>
  </li>
  <li><p>However, when I scroll down to about 25 items later, another file has the same background color even though it's not selected (it does not show up in Log.d as being selected, nor was it in the test ArrayList that was used to store selected files).</p>
  </li>
  <li><p>It seems as though the item is only retaining the background change of the previous occupant.</p>
  </li>
  </ul>
  </li>
  <li><p>Solution attempts:</p>
  <ul>
  <li><p>Previously, only the variables related to each list item were declared in the RecyclerView ViewHolder class and all changes were made in the onBindViewHolder method. Now, all changes to be made have been moved to the ViewHolder class inside a method called bind. There was no change in behavior.</p>
  </li>
  <li><p>If I set the default background image during the very first step in onBindViewHolder, the behavior changes such that the items do not retain changes of previous occupants. However, on scrolling back, the background change for the target item reverts to the default background image.</p>
  </li>
  </ul>
  </li>
  <li><p>Code:</p>
  </li>
  </ul>
  <pre><code>public class RVA extends RecyclerView.Adapter&lt;RVA.RVH&gt;
  {
      private LayoutInflater inf;
      private ArrayList&lt;File&gt; items;
  
      // The var below is used to track the no. of selected items
      // globally within the RVA class.
      private int numberOfSelectedItems = 0; 
  
      public RVA(ArrayList&lt;File&gt; _items)
      {
          items = _items;
      }
  
      @Override
      public RVA.RVH onCreateViewHolder(ViewGroup parent, int viewType)
      {
          inf = LayoutInflater.from(parent.getContext());
          return new RVH(inf, parent);
      }
  
      @Override
      public void onBindViewHolder(RVA.RVH holder, int position)
      {
          File listItem = items.get(position);
  
          // 'binding' each file element to a respective host container.
          holder.bind(listItem);
      }
  
      @Override
      public int getItemCount()
      {
          return items.size();
      }
  
      @Override
      public long getItemId(int position)
      {
          return position;
      }
  
      // The ViewHolder class.
      // Initially it was just declared as class.
      // There was no change observed after the 'final' modifier was added.
      final class RVH extends RecyclerView.ViewHolder
      {
          private Context context;
          private LinearLayout itemSelector;
          private ImageView itemIcon;
          private TextView itemName;
          private TextView itemSize;
  
          public RVH(LayoutInflater inf, ViewGroup parent)
          {
              super(inf.inflate(R.layout.list_item, parent, false));
              context = parent.getContext();
  
              // This is the SECOND outermost LinearLayout of each file item View.
              // It was previously the parent Layout, but there was no difference due to change.
              itemSelector = itemView.findViewById(R.id.item_selector);
  
              // This is the icon ImageView.
              itemIcon = itemView.findViewById(R.id.item_icon);
  
              // These are the data TextViews.
              itemName = itemView.findViewById(R.id.item_id);
              itemSize = itemView.findViewById(R.id.item_size);
          }
  
          // The 'bind' method that registers changes.
          public void bind(File fileItem)
          {
              String listItemName = fileItem.getName();
              itemName.setText(listItemName);
  
              //---- These are just changes to the icons depending on type. Works fine.
              if(fileItem.isDirectory())
              {
                  itemIcon.setImageResource(R.drawable.directory_icon);
                  itemSize.setText(&quot;Directory&quot;);
              }
              else
              {
                  itemSize.setText(fileItem.length() + &quot; B&quot;);
                  if(listItemName.endsWith(&quot;.jpg&quot;) || listItemName.endsWith(&quot;.jpeg&quot;) || listItemName.endsWith(&quot;.png&quot;) || listItemName.endsWith(&quot;.gif&quot;))
                  {
                      Glide.with(context).load(fileItem).centerCrop().into(itemIcon);
                  }
                  else
                  {
                      itemIcon.setImageResource(R.drawable.file_icon);
                  }
              }
              //---- END
  
              //---- This is the code which handles opening files according to type. Works fine.
              itemSelector.setOnClickListener(new View.OnClickListener()
              {
                  @Override
                  public void onClick(View v)
                  {
                      if(numberOfSelectedItems == 0)
                      {
                          if(!itemSize.getText().toString().endsWith(&quot; B&quot;))
                          {
                              Intent loadListItemIntent = new Intent(context, DirectoryViewActivity.class);
                              loadListItemIntent.putExtra(&quot;ITEMPATH&quot;, fileItem.getPath());
                              context.startActivity(loadListItemIntent);
                          }
                          else
                          {
                              if(itemName.getText().toString().endsWith(&quot;.jpg&quot;) || itemName.getText().toString().endsWith(&quot;.jpeg&quot;) || itemName.getText().toString().endsWith(&quot;.png&quot;) || itemName.getText().toString().endsWith(&quot;.gif&quot;))
                              {
                                  Glide.with(context).load(fileItem).centerCrop().into(itemIcon);
                                  Intent loadListItemIntent = new Intent(context, ImageActivity.class);
                                  loadListItemIntent.putExtra(&quot;ITEMPATH&quot;, fileItem.getPath());
                                  context.startActivity(loadListItemIntent);
                              }
                              else
                              {
                                  Toast.makeText(context, &quot;File needs proper application.&quot;, Toast.LENGTH_SHORT).show();
                              }
                          }
                      }
                  }
              });
              //---- END
  
  //---- !!! THIS SECTION is where the problem manifests.
              itemIcon.setOnClickListener(new View.OnClickListener()
              {
                  @Override
                  public void onClick(View v)
                  {
                      if(itemIcon.getTag().toString().equals(&quot;not_selected&quot;))
                      {
                          // Incrementing based on selection.
                          ++numberOfSelectedItems;
  
                          // Using a tag to identify/ denote whether item is selected.
                          itemIcon.setTag(&quot;selected&quot;);
  
                          // Changing the background &amp; disabling file opening while in selection mode.
                          itemSelector.setBackgroundResource(R.drawable.list_item_selected);
                          itemSelector.setClickable(false);
                          itemSelector.setLongClickable(false);
  
                          // I use this odd method to send a message to the host Activity
                          // that we have entered selection mode &amp; that the Activity should
                          // display some option buttons on the Action Bar.
                          if(context instanceof DirectoryViewActivity)
                          {
                              ((DirectoryViewActivity)context).addSelectedItem(fileItem);
                              if(numberOfSelectedItems == 1)
                              {
                                  ((DirectoryViewActivity)context).setSelectionMode();
                              }
                          }
                      }
                      else
                      {
                          // Decrementing based on deselection.
                          --numberOfSelectedItems;
  
                          // Overwiting the tag to identify/ denote item is now unselected.
                          itemIcon.setTag(&quot;not_selected&quot;);
  
                          // Background changed back to default &amp; file opening re-enabled.
                          itemSelector.setClickable(true);
                          itemSelector.setLongClickable(true);
                          itemSelector.setBackgroundResource(R.drawable.list_item_background);
  
                          // I use this method to send a message to the host Activity
                          // that we have exited selection mode &amp; that the Activity should
                          // remove related option buttons from the Action Bar.
                          if(context instanceof DirectoryViewActivity)
                          {
                              ((DirectoryViewActivity)context).removeSelectedItem(fileItem);
                              if(numberOfSelectedItems == 0)
                              {
                                  ((DirectoryViewActivity)context).voidSelectionMode();
                              }
                          }
                      }
                  }
              });
          }
      }
  }
  </code></pre>