title: Dynamic LinearLayout + Touch Event always use the last LinearLayout added
body: <p>I'm tying to add a list of dynamic "buttons" to a Layout. right now my buttons consist of a LinearLayout, ImageView and TextView. I add a touch even to the LinearLayout on each iteration. All objects are added and display correctly. But everything goes wrong from there.</p>
  
  <p>The touch event will fire for each button, but the corresponding sender object is the last LinearLayout added.</p>
  
  <p>For example: I have 3 "buttons" to add. Each are created and added to where they need to be. In the loop I add an event handler to the Touch event for each "button". When I go to touch the first one, the last one highlights. I touch the second one, the last one highlights.</p>
  
  <p>I am using a common method for the touch event and identifying them apart by a secondary value I store (like a tag).</p>
  
  <p>Provide below is the relevant code to this situation.</p>
  
  <p>Code for adding the "buttons" based on a record set from a web service.</p>
  
  <pre><code>if (calendars != null &amp;&amp; calendars.Rows.Count &gt; 0)
  {
      LinearLayout buttonCalendar = null;
      TextView labelTitle = null;
      ImageView imageIcon = null;
  
      Int64 App_CalendarID = 0;
      String Title = "";
  
      LinearLayout.LayoutParams layoutParams =
          new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, Android.DpToPx(45, this.View.Context));
  
      LinearLayout.LayoutParams imageParams =
          new LinearLayout.LayoutParams(Android.DpToPx(45, this.View.Context), LinearLayout.LayoutParams.MatchParent);
  
      LinearLayout.LayoutParams labelParams =
          new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent);
  
      Drawable icon = null;
  
      icon = Resources.GetDrawable(Resource.Drawable.image_calendar_light);
  
      TransitionDrawable buttonTransition = 
          (TransitionDrawable)Resources.GetDrawable(Resource.Drawable.button_transition);
  
      foreach (DataRow row in calendars.Rows)
      {
          App_CalendarID = Convert.ToInt64(row["App_CalendarID"]);
          Title = Convert.ToString(row["Title"]);
  
          buttonCalendar = new LinearLayout(layoutFeatureBody.Context)
          {
              Orientation = Orientation.Horizontal,
              LayoutParameters = layoutParams,
              Background = buttonTransition,
              Id = (Int32)App_CalendarID,
          };
  
          imageIcon = new ImageView(layoutFeatureBody.Context)
          {
              Background = icon,
              LayoutParameters = imageParams,
          };
          imageIcon.SetScaleType(ImageView.ScaleType.CenterInside);
  
          buttonCalendar.AddView(imageIcon);
  
          labelTitle = new TextView(layoutFeatureBody.Context)
          {
              Text = Title,
              TextSize = 14f,
              LayoutParameters = labelParams,
              Gravity = GravityFlags.CenterVertical,
          };
          labelTitle.SetTextColor(Color.White);
  
          buttonCalendar.AddView(labelTitle);
  
          layoutFeatureBody.AddView(buttonCalendar);
  
          buttonCalendar.Touch += buttonCalendar_Touch;
      }
  }
  </code></pre>
  
  <p>Below is the method assigned for the Touch event.</p>
  
  <pre><code>void buttonCalendar_Touch(object sender, View.TouchEventArgs e)
  {
      LinearLayout buttonLayout = (LinearLayout)sender;
  
      Android.TransitionButton(ref buttonLayout, e.Event);
  
      if (e.Event.Action != MotionEventActions.Up)
          return;
  
  }
  </code></pre>
  
  <p>My guess events don't work the same as they do in .Net :-X I'm struggling finding any relevant help to this exact situation online already.</p>
  
  <p>Thanks in advance.</p>