回收站View的网格布局管理器 [英] Recycler View With Gridlayout Manager

查看:238
本文介绍了回收站View的网格布局管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 RecyclerView 网​​格布局经理做出这样的旅行座位预订布局。

I'm using RecyclerView with GridLayout manager to make seat booking layout like travels.

我的问题是如何自动检测spancount时,我的专栏是随机的?

My problem is how to auto detect spancount when my column is random?

我知道 spanCount = recyclerViewWidth / singleItemWidth; ,但问题是 singleItemWidth 则不同,座椅宽度是不同的。

I know spanCount = recyclerViewWidth / singleItemWidth;, but the problem is singleItemWidth is different, as the seat width is different.

这是我想所需的布局:

输入图像的描述在这里

...但我目前得到的东西是这样的:

... but I'm currently getting something like this:

输入图像的描述在这里

这是在code我使用安排的座位布局:

This is the code I'm using to arrange the seat layout:

    gridLayoutManager=new GridLayoutManager(this,totalRows, LinearLayoutManager.VERTICAL,
            false);
    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            int rows=Integer.parseInt(upperlist.get(position).get("row"));
             return (rows);///(int) (mRecyclerView.getMeasuredWidth()/ 10);
        }
    });
     mRecyclerView.setLayoutManager( gridLayoutManager);
    MyAdapter myAdapter=new MyAdapter(this,gridArray);

...其中, upperlist 持有这些值:

 [{cols=8, seatNumber=29U, row=4, zindex=1}, 
  {cols=6,seatNumber=23U, row=4, zindex=1},
  {cols=4,seatNumber=21U,row=4, zindex=1}, 
  {cols=2, seatNumber=11U, row=4, zindex=1}, 
  {cols=0,seatNumber=9U,row=4, zindex=1},

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

  {cols=8, seatNumber=26U, row=1, zindex=1},
  {cols=6,seatNumber=18U, row=1, zindex=1}, 
  {cols=4,seatNumber=14U, row=1, zindex=1},
  {cols=2,seatNumber=6U,  row=1, zindex=1}, 
  {cols=0,seatNumber=2U,  row=1, zindex=1}]

这是一个列表,在这里我有行和列不同的值。因此,我需要一个可以在所有的布局工作,推广方法。

This is a single list, where I'm having different values for rows and columns. I therefore need a generalized method which can work on all layouts.

推荐答案

我猜COLS参数definies列的项目应该放在,对不对? 如果是那样的话,你应该采取的所有项目,此参数的最大值。 在这种情况下,10。由于所有项目似乎跨越2 cloumns,每个项目都有2的跨度尺寸和最大跨度指数为11。因此您的跨度数应为12(indizes从0至11)。

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

现在你必须要搞清楚的空白。例如。没有项目在所有的第3行,并根据您的数据,行1和; 4有在最后一个空元素。所以,你应该定义一个空项目。(! - > viewtype)的重新$ P $在布局psents一个空格

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
]

和修改剩余code以这种方式:

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}

您可以不是修改你的SpanSizeLookup这样的:

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

这篇关于回收站View的网格布局管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆