title: Determine if item is visible on RecyclerView
body: <p>I have built a basic interface consisting of a list of <code>CardView</code>’s inside a <code>RecyclerView</code>. When a card is added I need to know if it is visible on screen or not. </p>
  
  <p>I am trying to obtain this by using the layout manager’s <code>findLastVisibleItemPosition()</code> method, however it seems to return a result one item less than expected. For example if I have 1 card which is visible <code>findLastVisibleItemPosition()</code> returns <code>-1</code> (where I would expect the index of the visible card would be <code>0</code>).</p>
  
  <p>My simplified core methods are as follows:</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      items = new ArrayList&lt;&gt;();
  
      RecyclerView recycList = (RecyclerView) findViewById(R.id.rv);
      recycList.setHasFixedSize(true);
      llm = new LinearLayoutManager(this);
      llm.setOrientation(LinearLayoutManager.VERTICAL);
      recycList.setLayoutManager(llm);
      recycList.addOnChildAttachStateChangeListener(new ChildAttachListener(llm));
      adapter = new RecycAdapter(items);
      recycList.setAdapter(adapter);
  }
  
  private void addItem(){
      items.add(new Item());
      adapter.notifyDataSetChanged();
  }
  
  private class ChildAttachListener implements OnChildAttachStateChangeListener{
      LinearLayoutManager llm;
  
      public ChildAttachListener(LinearLayoutManager llm){
          super();
          this.llm = llm;
      }
  
      @Override
      public void onChildViewAttachedToWindow(View view) {
  
          System.out.println("Items size = "+items.size() + ", Last Visible Item = "+llm.findLastVisibleItemPosition());
      }
  
      @Override
      public void onChildViewDetachedFromWindow(View view) {
  
      }
  }
  </code></pre>
  
  <p>Return when first item is added:</p>
  
  <pre><code>Items size = 1, Last Visible Item = -1
  </code></pre>
  
  <p>Return when second item is added:</p>
  
  <pre><code>Items size = 2, Last Visible Item = 0
  Items size = 2, Last Visible Item = 0
  </code></pre>
  
  <p>Return when third item is added:</p>
  
  <pre><code>Items size = 3, Last Visible Item = 0
  Items size = 3, Last Visible Item = 1
  Items size = 3, Last Visible Item = 1
  </code></pre>
  
  <p>In all cases all items are visible (I can have 7 visible items on screen).</p>
  
  <p>Could someone please help me understand the return of <code>findLastVisibleItemPosition()</code> and suggest how I can determine if a card is visible on screen when adding it?</p>
  
  <p><strong>EDIT:</strong>
  I should clarify that my real objective is to know if an item is visible  when I add it (using my <code>addItem()</code> method). If there is a simple way of doing that inside the <code>addItem()</code> method even better.</p>