title: Android ListView not setting background color of first item after scrolling down
body: <p>I have a list view and a custom adapter which holds 10 items and shows 6 items at a time, until the user scrolls down to reveal the last few items. As the items are being selected by my code I am changing the background color of each specific item one after the other, and it works fine until I hit the last item and use:</p>
  
  <pre><code>mailList.smoothScrollToPosition(0);
  </code></pre>
  
  <p>to scroll back up to the top of the list. When this happens the background color for item 0 does not change, and then once I select the next item that background color changes and it continues to work perfectly.</p>
  
  <p>To change the background color of each item I am using this method:</p>
  
  <pre><code>public void selectRow(int position){
  
      // Get the row
      View row = getViewByPosition(position, mailList);
      // Highlight background colour
      row.setBackgroundColor(ContextCompat.getColor(activity, R.color.Pallette_Peach_CC));
      // For every other row apart from 'position'
      // Check if read/unread and set background colour
      View v;
      for(int i = 0; i &lt; mailList.getCount(); i++){
          if(i != position){
              v = getViewByPosition(i, mailList);
              if(Consts.mailBox.get(i).isRead()){
                  v.setBackgroundColor(ContextCompat.getColor(activity, R.color.Ivory_Transparent_55));
              }else{
                  v.setBackgroundColor(ContextCompat.getColor(activity, R.color.Ivory_Transparent_77));
              }
          }
      }
  
  }
  </code></pre>
  
  <p>which uses the method getViewByPosition() to return the view so I can set it's color:</p>
  
  <pre><code>// returns a specific row
  private View getViewByPosition(int pos, ListView listView) {
      final int firstListItemPosition = listView.getFirstVisiblePosition();
      final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
  
      if (pos &lt; firstListItemPosition || pos &gt; lastListItemPosition ) {
          return listView.getAdapter().getView(pos, null, listView);
      } else {
          final int childIndex = pos - firstListItemPosition;
          return listView.getChildAt(childIndex);
      }
  }
  </code></pre>
  
  <p>On the first iteration item 0 changes color, but once I reach the end of the list (where list positions 9(the end of the list) up to 3 are visible) and smoothScroll to the first (0) position, the color of item 0 does not change. To call the selectRow method I use:</p>
  
  <pre><code>if(position == 0) {
      mailList.smoothScrollToPosition(0);
      selectRow(0);
  }
  </code></pre>
  
  <p>So it should scroll back up to the top of the list, call selectRow(0) and work like it did at the first iteration, but it doesn't seem to be doing that. Can anyone help me out? Thanks.</p>
  
  <p>EDIT:</p>
  
  <p>Here is my getView method from my adapter class:</p>
  
  <pre><code>@Override
  public View getView(int position, View convertView, ViewGroup parent) {
      View view = convertView;
      if(view == null){
          view = inflater.inflate(R.layout.fragment_mail_row, null);
      }
  
      LinearLayout rowContainer = (LinearLayout) view.findViewById(R.id.mailRowContainer);
      ImageView mailIcon = (ImageView) view.findViewById(R.id.mailRowImage);
      TextView mailTitle = (TextView) view.findViewById(R.id.mailRowTitleText);
      TextView mailDate = (TextView) view.findViewById(R.id.mailRowTimeText);
  
      MailItem mMailItem = mail.get(position);
      String title = mMailItem.getTitle();
  
      String timestamp;
      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
      Date today = new Date();
      Date messageDate = mMailItem.getDate();
      if(sdf.format(today).equals(sdf.format(messageDate))){
          // If date of message is today then display the time
          SimpleDateFormat tf = new SimpleDateFormat("kk:mm");
          timestamp = tf.format(messageDate);
      }else{
          SimpleDateFormat df = new SimpleDateFormat("dd/MM/yy");
          timestamp = df.format(messageDate);
      }
  
      // If mail is already opened, re-set the background colour
      if(mMailItem.isRead()){
          rowContainer.setBackgroundColor(ContextCompat.getColor(activity, R.color.Ivory_Transparent_55));
      }else{
          rowContainer.setBackgroundColor(ContextCompat.getColor(activity, R.color.Ivory_Transparent_77));
      }
  
      // Set the title and time
      mailTitle.setText(title);
      mailDate.setText(timestamp);
      if(mMailItem.getType().equals(Consts.MAIL_TYPE_AUDIO)){
          if(mMailItem.isRead()){
              mailIcon.setImageResource(R.mipmap.ic_mail_audio_open);
          }else{
              mailIcon.setImageResource(R.mipmap.ic_mail_audio_closed);
          }
      }else{
          if(mMailItem.isRead()){
              mailIcon.setImageResource(R.mipmap.ic_mail_opened);
          }else{
              mailIcon.setImageResource(R.mipmap.ic_mail_closed);
          }
      }
  
      return view;
  }
  </code></pre>
  
  <p>EDIT 2:</p>
  
  <p>For further clarity, the selectRow method is being called from a seperate class called MailSelector which is in its entirety here:</p>
  
  <pre><code>public class MailSelector {
  
      double noOfItems;
      double noOfItemsPerPage;
      int noOfPages;
      int noOfItemsOnLastPage;
      private ListView mailList;
      private Activity activity;
  
      public MailSelector(Activity a, ListView list){
          this.activity = a;
          this.mailList = list;
          noOfItemsPerPage = mailList.getLastVisiblePosition() - mailList.getFirstVisiblePosition();
      }
  
      public int nextRow(int position){
          // Calculate item/page info
          noOfItems = mailList.getCount();
          if(noOfItems &lt;= noOfItemsPerPage ){
              // Only one page so iterate and return to start position
              // If only one item or if after last item, select position 0
              if(mailList.getChildCount() == 1 || position &gt;= mailList.getCount()){
                  selectRow(0);
                  return 0;
              }
          }else{
              // More than one page so handle scrolling
              noOfPages = (int) Math.ceil(noOfItems / noOfItemsPerPage);
              // Calculate current page. Round down and + 1, as page 1 is actually 0
              int currentPage = getCurrentPage(position);
              int curPageStartPos = (int) ((int) (noOfItemsPerPage * currentPage) - noOfItemsPerPage);
              int curPageEndPos = curPageStartPos + ((int) noOfItemsPerPage - 1);
              noOfItemsOnLastPage = (int)(noOfItems % noOfItemsPerPage);
  
              // Scrolling if on last item of page
              if(position == curPageEndPos){
                  // Check if last page
                  if(currentPage == noOfPages){
                      // Scroll back to position 0
                      mailList.smoothScrollToPosition(0);
                      selectRow(0);
                      return 0;
                  }else if(currentPage == noOfPages - 1){
                      // If page before last
                      // Get last position of last page
                      int lastPosOfNextPage = mailList.getCount() - 1;
                      mailList.smoothScrollToPosition(lastPosOfNextPage);
                      position = (int) ((position + 1) % noOfItems);
                      selectRow(position);
                  }else{
                      int lastPosOfNextPage = (int) (position + noOfItemsPerPage);
                      mailList.smoothScrollToPosition(lastPosOfNextPage);
                      position = (int) ((position + 1) % noOfItems);
                      selectRow(position);
                  }
              }else{
                  position = (int) ((position + 1) % noOfItems);
                  selectRow(position);
              }
  
          }
          if(position == 0) {
              mailList.smoothScrollToPosition(0);
              selectRow(0);
          }
          return position;
      }
  
      public void selectRow(int position){
  
          // Get the row
          View row = getViewByPosition(position, mailList);
          // Highlight background colour
          row.setBackgroundColor(ContextCompat.getColor(activity, R.color.Pallette_Peach_CC));
          // For every other row apart from 'position'
          // Check if read/unread and set background colour
          View v;
          for(int i = 0; i &lt; mailList.getCount(); i++){
              if(i != position){
                  v = getViewByPosition(i, mailList);
                  if(Consts.mailBox.get(i).isRead()){
                      v.setBackgroundColor(ContextCompat.getColor(activity, R.color.Ivory_Transparent_55));
                  }else{
                      v.setBackgroundColor(ContextCompat.getColor(activity, R.color.Ivory_Transparent_77));
                  }
              }
          }
  
      }
  
      // returns a specific row
      private View getViewByPosition(int pos, ListView listView) {
          final int firstListItemPosition = listView.getFirstVisiblePosition();
          final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
  
          if (pos &lt; firstListItemPosition || pos &gt; lastListItemPosition ) {
              return listView.getAdapter().getView(pos, null, listView);
          } else {
              final int childIndex = pos - firstListItemPosition;
              return listView.getChildAt(childIndex);
          }
      }
  
      private int getCurrentPage(int position){
          return ((int) Math.floor(position / (double) noOfItemsPerPage)) + 1;
      }
  
  
  }
  </code></pre>