title: RecyclerView With Multiple Layouts
body: <p>I am using RecyclerView to display text in CardView but I want to add a Header TextView at the top of the RecyclerView that looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/hdvLO.jpg" alt="enter image description here"></p>
  
  <p>So I made another layout file "top_header.xml" with just a TextView but I am not sure how to modify the adapter to get both layouts in there.</p>
  
  <pre><code>public class Adapter extends RecyclerView.Adapter&lt;Adapter.ViewHolder&gt; {
  
  List&lt;AdapterData&gt; mItems;
  
  public Adapter() {
      super();
      mItems = new ArrayList&lt;AdapterData&gt;();
      AdapterData data = new AdapterData();
      data.setCode("dummytext");
      data.setResult("dummytext");
      mItems.add(data);
  
      data = new AdapterData();
      data.setCode("dummytext");
      data.setResult("dummytext");
      mItems.add(data);
  
      data = new AdapterData();
      data.setCode("dummytext");
      data.setResult("dummytext");
      mItems.add(data);
  
      data = new AdapterData();
      data.setCode("dummytext");
      data.setResult("dummytext");
      mItems.add(data);
  
      data = new AdapterData();
      data.setCode("dummytext");
      data.setResult("dummytext");
      mItems.add(data);
  
  }
  
  @Override
  public int getItemViewType(int position) {
      int viewType = 0;
      return viewType;
  }
  
  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      if (viewType == 0) {
          return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.top_textview, parent, false));
      }
  
      return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_card_item, parent, false));
  }
  
  @Override
  public void onBindViewHolder(ViewHolder viewHolder, int position) {
      AdapterData data = mItems.get(position);
      viewHolder.code.setText(data.getCode());
      viewHolder.result.setText(data.getResult());
  
  }
  
  
  @Override
  public int getItemCount() {
      return mItems.size();
  }
  
  
  class ViewHolder1 extends RecyclerView.ViewHolder{
      public TextView header;
  
      public ViewHolder1(View itemView) {
          super(itemView);
          header = (TextView)itemView.findViewById(R.id.header_textview);
  
      }
  }
  
  class ViewHolder2 extends RecyclerView.ViewHolder{
      public TextView code;
      public TextView result;
  
      public ViewHolder2(View itemView) {
          super(itemView);
          code = (TextView)itemView.findViewById(R.id.sims_code);
          result = (TextView)itemView.findViewById(R.id.sims_result);
  
      }
  }
  
  
  }
  </code></pre>