title: Changing background color of ListView item upon clicking a Button
body: <p>Apologies if this question has been asked previously.</p>
  
  <p>I have a ListFragment that contains a Button in each list item. Clicking on the Button should change the background color of that particular list item. I understand that the ListView inside the ListFragment refreshes itself implying that if a user scrolls it is likely that a list item that was not previously clicked will have its background color changed. </p>
  
  <p>Many of the solutions I have come across involve storing the position of the list item that was clicked in an overridden getView(int position, View convertView, ViewGroup parent) method implemented in a custom adapter (that extends SimpleAdapter in my case). </p>
  
  <p>However my problem is compounded by the presence of a Button inside every list item implying  that the Button will not be clickable unless I attach an OnClickListener inside a custom adapter (this part is done). Now, trying to store the position of the Button's list item fails because the saved position is always a refreshed position and not the absolute position of the list item. I also tried setting tags for each button but even those get recycled as the page scrolls, which is (unfortunately) expected behaviour considering the design of ListView.</p>
  
  <p>I understand that this problem is easily solvable if the a list item does not have children that need to be clicked. But what if there are children within every list item that need to respond to clicks separate from the parent view that contains each list item ? I have also tried using selectors to manipulate a list item's background but that hasn't helped either.</p>
  
  <p>Here is my Custom Adapter's getView method:</p>
  
  <pre><code>@Override
  public View getView(int position, View convertView, ViewGroup parent){
  
      View view = convertView;
      final int pos = position;
  
      if(view == null){
  
          LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          view = inflater.inflate(R.layout.fragment_layout, null);
  
          Button button = (Button) view.findViewById(R.id.button);
          button.setOnClickListener(new View.OnClickListener(){
  
  
              @Override
              public void onClick(View v) {
  
                  Log.d("CLICKED POSITION",Integer.valueOf(pos).toString());
                  View parent = (View) v.getParent();
  
                  parent.setBackgroundColor(Color.GREEN);
  
              }
          });
  
      }
  
  
      return view;
  }
  </code></pre>
  
  <p>And the XML layout file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="300dp"
  android:layout_height="match_parent"
  android:orientation="vertical" 
  android:layout_weight="1"
  android:descendantFocusability="blocksDescendants"
  android:layout_margin="5dp" 
  &gt;
  
  &lt;ListView android:id="@id/android:list"
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
  
  /&gt; 
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/label" 
          android:layout_gravity="start"
      /&gt;
  
      &lt;TextView 
          android:id="@+id/text_id"
          android:layout_width="0dp"
          android:layout_height="wrap_content" 
          android:layout_weight="1"
          android:layout_gravity="end"
      /&gt;
  
       &lt;Button
           android:id="@+id/button"
           android:layout_height="wrap_content"
           android:layout_width="wrap_content"
           android:text="@string/button"
           android:layout_margin="2dp"
           android:layout_marginLeft="2dp"
           android:layout_marginRight="2dp"
           android:clickable="true"
  
        /&gt;
  
  
  &lt;/LinearLayout&gt; 
  </code></pre>
  
  <p>I have spent many hours on this problem and I am unable to arrive at a solution. Any direction, guidance or a solution would be most appreciated.</p>