title: Changing layout managers for different views in RecyclerView
body: <p>I implemented an expandable recyclerview with child elements that are part of the list. I followed this <a href="http://anandbose.github.io/android_recyclerview_expandablelistview.html" rel="noreferrer">code</a>. This is how it works, </p>
  
  <blockquote>
    <p>The implementation of ExpandableListView using RecyclerView is briefly described as follows. The list model has an additional parameter "type" that identifies whether the item is a header or child. By checking this parameter, the adapter inflates view and viewholder corresponding to the type. If the type is HEADER, it will inflate the layout of header item, that contains a TextView and a ImageView for indicating whether the child tree is expanded or not.</p>
  </blockquote>
  
  <p>Now, what I want to do is make the expanded layout a grid. I would normally do this by setting the layout manager to GridLayoutManager but in this case, I am only using one recyclerview, which means that I can't change the layout manager without changing the header which ends up causing the entire recyclerview to turn into a grid including the headers. </p>
  
  <p>My question is: how would you change the layout manager for only a couple layouts inside the adapter?</p>
  
  <p>Edit: I added some code.</p>
  
  <p>Recyclerview adapter:</p>
  
  <pre><code>public class ExpandableListAdapter extends RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt; {
  
  // These are constants that are used to determine if the item is a child or a header and is defined with each item from the data model
  public static final int HEADER = 0;
  public static final int CHILD = 1;
  
  private List&lt;Item&gt; data;
  
  public ExpandableListAdapter(List&lt;Item&gt; data) {
      this.data = data;
  }
  
  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) {
      View view = null;
  
      LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
      // Check whether the item is a header or child and inflate differnet layouts
      switch (type) {
          case HEADER:
              // Inflate a header layout if the item is a header
              view = inflater.inflate(R.layout.list_header, parent, false);
              ListHeaderViewHolder header = new ListHeaderViewHolder(view);
              return header;
          case CHILD:
              // Inflate a child layout if the item is a child
              view = inflater.inflate(R.layout.list_child, parent, false);
              ListChildViewHolder child = new ListChildViewHolder(view);
              return child;
      }
      return null;
  }
  
  public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
      final Item item = data.get(position);
  
      // Bind different layouts based on if the item is a header or child
      switch (item.getType()) {
          case HEADER:
              // ...
          case CHILD:
              // ...
      }
  }
  
  @Override
  public int getItemViewType(int position) {
      return data.get(position).type;
  }
  
  @Override
  public int getItemCount() {
      return data.size();
  }
  
  // Viewholder for the header items
  private static class ListHeaderViewHolder extends RecyclerView.ViewHolder {
      // ...
  }
  
  // Viewholder for the child items
  private static class ListChildViewHolder extends RecyclerView.ViewHolder {
      // ...
  }
  </code></pre>
  
  <p>And this is the main activity where I declare the layout manager:</p>
  
  <pre><code>recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
  recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
  </code></pre>