title: How to make a GridView with headers synchronized to TabLayout?
body: <p>I'm having trouble creating a grid view with headers. And these headers still need to be synchronized with TabLayout as the screen scrolls.</p>
  <p>So far I've managed to create the view of items with RecyclerView and TabLayout. Now I need to add these headers to the grid and synchronize with the tab position.</p>
  <p>Tried the following on the adapter:</p>
  <pre><code>View view;
  if (viewType == ITEM_VIEW_TYPE_HEADER) {
   view = LayoutInflater.from(context).inflate(R.layout.view_header, parent, false);
  } else {
   view = LayoutInflater.from(context).inflate(R.layout.items_listitem, parent, false);
  }
  return new AdapterBuscaProduto.ViewHolder(view);
  </code></pre>
  <p>In Activity it looks like this:</p>
  <pre><code>GridLayoutManager glm = new GridLayoutManager(getContext(), 3);
  glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
  @Override 
  public int getSpanSize(int position) {
      return (position == 0) ? 3 : 1;
    }
  });
  </code></pre>
  <p>On Json the return is as follows:</p>
  <pre><code>    {
      &quot;resposta&quot;:[
      {
       &quot;cd_servico&quot;:&quot;1&quot;,
       &quot;ds_servico&quot;:&quot;HEADER 1&quot;,
       &quot;ITEMS&quot;:[
          {
            &quot;ds_descricao1&quot;:&quot;DESCRICAO SERVICO&quot;,
            &quot;ds_descricao2&quot;:&quot;DESCRICAO 2&quot;,
            &quot;ds_valor&quot;:&quot;120.00&quot;
          },
          {
            &quot;ds_descricao1&quot;:&quot;DESCRICAO SERVICO&quot;,
            &quot;ds_descricao2&quot;:&quot;DESCRICAO 2&quot;,
            &quot;ds_valor&quot;:&quot;65.00&quot;
          }
          ]
      },
      {
        &quot;cd_servico&quot;:&quot;2&quot;,
        &quot;ds_servico&quot;:&quot;HEADER 2&quot;,
        &quot;ITEMS&quot;:[
            {
              &quot;ds_descricao1&quot;:&quot;DESCRICAO&quot;,
              &quot;ds_descricao2&quot;:&quot;DESCRICAO&quot;,
              &quot;ds_valor&quot;:&quot;90.00&quot;
             }
             ]
        }
      ]}
  </code></pre>
  <p>Attempting to sync TabLayout with listing:</p>
  <pre><code>        recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
          @Override
          public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
              super.onScrollStateChanged(recyclerView, newState);
              if (newState == 0) {
                  isScrolled = false;
              } else {
                  isScrolled = true;
              }
          }
  
          @Override
          public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
              super.onScrolled(recyclerView, dx, dy);
              int itemPosition = gridLayoutManager.findFirstCompletelyVisibleItemPosition();
  
              if(itemPosition!= tabLayout.getSelectedTabPosition()){
                  tabLayout.getTabAt(itemPosition);
              }
          }
      });
  </code></pre>
  <p>What I need is something like this.</p>
  <p><a href="https://i.stack.imgur.com/DTRZN.gif" rel="nofollow noreferrer">example</a></p>