title: Android Background Selector Pressed State Shows On All Items At Once
body: <p>I have set up a background drawable selector so as to highlight the background of a list item when pressed (similarly to how I've done it many times before except the drawable is assigned in code).  First I'll show you the problem then put my code at the bottom.  If anyone can help me figure this out, I would be appreciate it!</p>
  
  <p>Here the screen is waiting for input:</p>
  
  <p><img src="https://i.stack.imgur.com/JAo0cl.png" alt="Waiting for input"></p>
  
  <p>Here I am pressing and holding down a single item.  If I slide my finger off the screen without lifting up over an item it remains just like this:</p>
  
  <p><img src="https://i.stack.imgur.com/Z9aMvl.png" alt="enter image description here"></p>
  
  <p>If I press and hold and then slide up to scroll the list the items that come into view are not displaying the pressed color:</p>
  
  <p><img src="https://i.stack.imgur.com/KXquIl.png" alt="enter image description here"></p>
  
  <p>Here is the relevant XML.  I set the drawable background here, but it really gets set in code so it's redundant.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="@dimen/thumbnail_size"
      android:background="@drawable/white_button_background"&gt;
  ...
  &lt;/Relative...
  </code></pre>
  
  <p>Here is the drawable XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;selector xmlns:android="http://schemas.android.com/apk/res/android" &gt;
  
      &lt;item android:drawable="@color/stitcher_holo_blue_light" android:state_pressed="true"/&gt;
       &lt;item android:drawable="@android:color/white"/&gt;
  
   &lt;/selector&gt;
  </code></pre>
  
  <p>Here is the code where the background selector is set in an array adapter:</p>
  
  <pre><code>public MyArrayAdapter(...) {
      super(...);
      mItemBackground = mResources.getDrawable(R.drawable.mybuttonselector);
  }
  
  public View getView(..., View convertView, ...) {
      ...
      convertView.setBackgroundDrawable(mItemBackground);//I also tried setBackground()
      ...
  }
  </code></pre>
  
  <p>Any ideas would be appreciated!</p>