Recycler View With Gridlayout Manager

心不动则不痛 提交于 2019-12-04 07:44:00

I guess the "cols" parameter definies the column the item should be placed, right? If that's the case, you should take the max value of all items for this parameter. In this case "10". As all items seem to span 2 cloumns, each item has a span-size of 2 and the max span index is 11. Therefore your span-count should be 12 (indizes from 0 to 11).

Now you'll have to figure out the empty spaces. E.g. there is no item at all in row 3 and according to your data, row 1 & 4 have an empty element at the end. So you should define a empty item (-> viewtype!) that represents a blank space in your layout.

Finally, sort the list of items by their row-index and column index, so that you achieve this structure:

[ //row 1
  {cols=0, seatNumber=2U, row=1, zindex=1},
  {cols=2, seatNumber=6U, row=1, zindex=1}, 
  {cols=4, seatNumber=14U, row=1, zindex=1},
  {cols=6, seatNumber=18U, row=1, zindex=1},
  {cols=8, seatNumber=26U, row=1, zindex=1},
  {cols=10, row=1, zindex=1}, //an empty element

  //row 2
  {cols=0, seatNumber=1U, row=2, zindex=1},
  {cols=2, seatNumber=5U,  row=2, zindex=1},  
  {cols=4, seatNumber=13U, row=2, zindex=1}, 
  {cols=6, seatNumber=17U, row=2, zindex=1},
  {cols=8, seatNumber=25U, row=2, zindex=1},  
  {cols=10, seatNumber=D2U,row=2, zindex=1}, 

  //row 3 (just empty elements)
  {cols=0, row=3, zindex=1},
  {cols=2, row=3, zindex=1},
  {cols=4, row=3, zindex=1},
  {cols=6, row=3, zindex=1},
  {cols=8, row=3, zindex=1},
  {cols=10, row=3, zindex=1},

  //row 4
  {cols=0, seatNumber=9U, row=4, zindex=1},
  {cols=2, seatNumber=11U, row=4, zindex=1},
  {cols=4, seatNumber=21U, row=4, zindex=1}, 
  {cols=6, seatNumber=23U, row=4, zindex=1},
  {cols=8, seatNumber=29U, row=4, zindex=1},
  {cols=10, row=4, zindex=1} //an empty element
]

And modify the remaining code in this way:

columnCount = computeTotalColumnCount(...); //
addEmptyElements(...); // add empty elements to your data structure (upperList or gridArray)

gridLayoutManager=new GridLayoutManager(this, columnCount, LinearLayoutManager.VERTICAL,
            false);
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return 2;
        }
    });

If you want the span-size of each items to be more flexible, you should add a new parameter to each item, so that an item looks like this:

{cols=0, seatNumber=2U, row=1, zindex=1, spansize=2}

You can than modify your SpanSizeLookup to this:

gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return Integer.parseInt(upperList.get(i).get("spansize"));
        }
   });

I hope this helps ;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!