title: How ListView's recycling mechanism works
body: <p>So I have this problem I had before, and naturally I asked for help on <a href="https://stackoverflow.com/questions/11919033/why-do-i-need-to-call-removeview-in-order-to-add-a-view-to-my-linearlayout/11920159#11920159">here</a>. Luksprog's answer was great because I had no idea about how ListView and GridView optimized itself with recycling Views. So with his advice I was able to change how I added Views to my GridView. Problem is now I have something that does not make sense. This is my <code>getView</code> from my <code>BaseAdapter</code>:</p>
  
  <hr>
  
  <pre><code>public View getView(int position, View convertView, ViewGroup parent) {
          if(convertView == null) {
              LayoutInflater inflater = LayoutInflater.from(parent.getContext());
              convertView = inflater.inflate(R.layout.day_view_item, parent, false);
          }
          Log.d("DayViewActivity", "Position is: "+position);
          ((TextView)convertView.findViewById(R.id.day_hour_side)).setText(array[position]);
          LinearLayout layout = (LinearLayout)convertView.findViewById(R.id.day_event_layout);
  
          //layout.addView(new EventFrame(parent.getContext()));
  
          TextView create = new TextView(DayViewActivity.this);
          LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 62, getResources().getDisplayMetrics()), 1.0f);
          params.topMargin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
          params.bottomMargin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
          create.setLayoutParams(params);
          create.setBackgroundColor(Color.BLUE);
          create.setText("Test"); 
          //the following is my original LinearLayout.LayoutParams for correctly setting the TextView Height
          //new LinearLayout.LayoutParams(0, (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60, getResources().getDisplayMetrics()), 1.0f)   
          if(position == 0) {
              Log.d("DayViewActivity", "This should only be running when position is 0. The position is: "+position);
              layout.addView(create);
          }
  
          return convertView;
      }
  
  }
  </code></pre>
  
  <hr>
  
  <p>Problem is when I scroll, this happens, and not on position 0... Looks like position 6 and position 8, plus it puts two in position 8. Now I am still trying to get the hang of using ListView and GridView so I do not understand why this is happening. One of the main reasons I am making this question is to help others who probably don't know about ListView and GridView's recycling View, or the way this <a href="http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/" rel="noreferrer">article</a> puts it, ScrapView mechanism.</p>
  
  <p><img src="https://i.stack.imgur.com/rvjJV.png" alt="enter image description here"></p>
  
  <p><strong>Later Edit</strong></p>
  
  <p>Adding link to a google IO talk that is basically all you need to understand how ListView works. Link was dead in on of the comments. So user3427079 was nice enough to update that link. <a href="https://www.youtube.com/watch?v=N6YdwzAvwOA" rel="noreferrer">Here</a> it is for easy access.</p>