title: Android RecyclerView switching layouts (grid/list) old viewHolders used
body: <p><strong>Issue Summary</strong></p>
  
  <p>I have a RecyclerView list in an activity with an actionBar button used to switch the RecyclerView between a LIST and a GRID layout.  </p>
  
  <p>The layouts of the items are different when viewing the list as a LIST and as a GRID.  </p>
  
  <p>When viewing the items in a LIST the row format is</p>
  
  <pre><code>|----|
  |icon|  Item Name
  |----|
  </code></pre>
  
  <p>When viewing the items in a GRID the row format is</p>
  
  <pre><code>|--------------|
  | large icon   |
  |              |
  |              |
  |              |      
  |--------------|
      Item Name
  </code></pre>
  
  <p>Therefore viewing my list as i LIST i see </p>
  
  <pre><code>|----|
  |icon|  Item1
  |----|
  |----|
  |icon|  Item2
  |----|
  |----|
  |icon|  Item3
  |----|
  |----|
  |icon|  Item3
  |----|
  </code></pre>
  
  <p>and when viewing my list as a GRID i see </p>
  
  <pre><code>|--------------|    |--------------|
  | large icon   |    | large icon   |      
  |              |    |              |      
  |              |    |              |      
  |              |    |              |      
  |--------------|    |--------------|
      Item1                Item2
  |--------------|    |--------------|
  | large icon   |    | large icon   |
  |              |    |              |
  |              |    |              |
  |              |    |              |      
  |--------------|    |--------------|
      Item3                Item4
  </code></pre>
  
  <p>When i use the actionBar button to toggle the list view from LIST to GRID or GRID to LIST, some views are still rendered in the layout for the other style.  I believe its the views which you scrolled out of sight before switching are recycled and reused rather than recreated.</p>
  
  <p>For example if i go from GRID to LIST i see</p>
  
  <pre><code>                    |--------------|
                      | large icon   |      
  |----|              |              |      
  |icon|  Item1       |              |      
  |----|              |              |      
                      |--------------|
                           Item2
  |--------------|    |--------------|
  | large icon   |    | large icon   |
  |              |    |              |
  |              |    |              |
  |              |    |              |      
  |--------------|    |--------------|
      Item3                Item4
  </code></pre>
  
  <p><strong>Background code information</strong></p>
  
  <p>I have 2 layout managers; one for each layout style</p>
  
  <pre><code>private RecyclerView.LayoutManager linearLayoutManager;
  private RecyclerView.LayoutManager gridLayoutManager;
  </code></pre>
  
  <p>In the adapter i store whether i am displaying the list as a grid or list</p>
  
  <pre><code>enum ListViewEnum{
      LIST,
      GRID
  }
  
  ListViewEnum listViewStyle = ListViewEnum.LIST;
  
  public ListViewEnum getListStyle() {
      return listViewStyle;
  }
  </code></pre>
  
  <p>The layout of the items in the list depend on whether the list is being rendered as a list or a grid. </p>
  
  <pre><code>ViewHolderList
  ViewHolderGrid
  </code></pre>
  
  <p>When creating the view holder, I base the type of View Holder on the adapters list view style</p>
  
  <pre><code>protected RecyclerView.ViewHolder getItemViewHolder(ViewGroup parent){
      if (listViewStyle == ListViewEnum.LIST){
          View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row, parent, false);
          return new ViewHolderList(itemView, ....);
      } else {
          View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_row, parent, false);
          return new ViewHolderGrid(itemView, ....);
      }
  }
  </code></pre>
  
  <p>When the user clicks the actionBar button i toggle the adapter's value</p>
  
  <pre><code>getListAdapter().setListViewMode(ListViewMode.LIST);
  </code></pre>
  
  <p>I then refresh </p>
  
  <pre><code>listView.setLayoutManager(getListAdapter().getListStyle() == ListViewEnum.LIST ? linearLayoutManager : gridLayoutManager);
  getListAdapter().notifyDataSetChanged();
  listView.invalidateItemDecorations();
  invalidateOptionsMenu();
  </code></pre>
  
  <p>I understand that i am refreshing the list adapter but it is not letting go of recycledViews. Debugging shows that on clicking the toggle button i see calls to just "onBindViewHolder" for some positions (the recycled ones) and "onCreateViewHolder" and  "onBindViewHolder"(for the new ones) therefore indicating that the old view holders are being recycled.</p>
  
  <p>How do i refresh the list so that it does not re-use ViewHolders from the wrong list style?</p>