How to set different columns for rows in android gridview

后端 未结 10 851
旧时难觅i
旧时难觅i 2020-12-01 07:17

I want to have a gridview similar to this

\"enter

Every odd numbered row will

10条回答
  •  难免孤独
    2020-12-01 08:22

    If you are using RecyclerView for GridView, then there is solution that should work for you:

    GridLayoutManager layoutManager = new GridLayoutManager(this, 4);
    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
             int mod = position % 6;    
    
             if(position == 0 || position == 1)
                  return 2;
             else if(position < 6)
                  return 1;
             else if(mod == 0 || mod == 1)
                  return 2;
             else
                  return 1;
        }
    });
    
    recyclerView.setLayoutManager(layoutManager);
    

    Hope this work for you!

提交回复
热议问题