1 year ago

#11526

test-img

Roberto

How to make a GridView with headers synchronized to TabLayout?

I'm having trouble creating a grid view with headers. And these headers still need to be synchronized with TabLayout as the screen scrolls.

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.

Tried the following on the adapter:

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);

In Activity it looks like this:

GridLayoutManager glm = new GridLayoutManager(getContext(), 3);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override 
public int getSpanSize(int position) {
    return (position == 0) ? 3 : 1;
  }
});

On Json the return is as follows:

    {
    "resposta":[
    {
     "cd_servico":"1",
     "ds_servico":"HEADER 1",
     "ITEMS":[
        {
          "ds_descricao1":"DESCRICAO SERVICO",
          "ds_descricao2":"DESCRICAO 2",
          "ds_valor":"120.00"
        },
        {
          "ds_descricao1":"DESCRICAO SERVICO",
          "ds_descricao2":"DESCRICAO 2",
          "ds_valor":"65.00"
        }
        ]
    },
    {
      "cd_servico":"2",
      "ds_servico":"HEADER 2",
      "ITEMS":[
          {
            "ds_descricao1":"DESCRICAO",
            "ds_descricao2":"DESCRICAO",
            "ds_valor":"90.00"
           }
           ]
      }
    ]}

Attempting to sync TabLayout with listing:

        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);
            }
        }
    });

What I need is something like this.

example

android

android-recyclerview

android-tablayout

android-gridview

android-gridlayout

0 Answers

Your Answer

Accepted video resources