Gridlayoutmanager,2x1排列

时间:2018-02-17 14:39:53

标签: android android-recyclerview gridlayoutmanager staggeredgridlayoutmanager

我正在尝试使用recyclerview中的gridlayoutmanager创建如下的UI:

+-------+ +-------+
|       | |       |
|       | |       |
|       | |       |
+-------+ +-------+
+-----------------+
|                 |
|                 |
+-----------------+

我在这里调整onBindViewHolder的宽度:

int mode = (position+1)%4;
        int h_mul = 1, w_mul = 1;
        switch (mode){
            case 0:
                h_mul = w_mul = 2;
                break;
            case 2:
                w_mul = 2;
                break;
            case 3:
            case 1:
            default:
                h_mul = w_mul = 1;
                break;
        }
        mCardView.getLayoutParams().height = h_mul * mContext.getResources().getInteger(R.integer.video_tile_height);
        mCardView.getLayoutParams().width = w_mul * mContext.getResources().getInteger(R.integer.video_tile_width);

我尝试了很多方法:  1.使用setSpanSizeLookup的水平gridlayoutmanager:

    manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            int mode = (position+1)%4;
            switch (mode){
                case 0:
                case 2:
                    return manager.getSpanCount();
                case 3:
                case 1:
                    return 1;
                default:
                    return -1;
            }
        }
    });

我最终得到的是:

+------------+  +-----------------------------+ +------------+
|            |  |                             | |            |
|            |  |                             | |            |
|            |  |                             | |            |
|            |  |                             | |            |
|            |  |                             | |            |
+------------+  +-----------------------------+ +------------+

如果我没有设置跨度,那么它最终会像这样:

+------------+               +---------------+
|            |               |               |
|            |               |               |
|            |               |               |
|            |               |               |
|            |               |               |
+------------+               +---------------+

+---------------------------+
|                           |
|                           |
|                           |
|                           |
|                           |
+---------------------------+

  1. 水平staggeredgridlayoutmanager。 如果使用

     StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
            layoutParams.setFullSpan(true);
    
  2. 然后它变得与

    相同
    +------------+  +-----------------------------+ +------------+
    |            |  |                             | |            |
    |            |  |                             | |            |
    |            |  |                             | |            |
    |            |  |                             | |            |
    |            |  |                             | |            |
    +------------+  +-----------------------------+ +------------+
    
    

    如果我最初不使用全跨度它是完美的但如果我滚动Recyclerview它变得与上面相同:

    +------------+  +-----------------------------+ +------------+
    |            |  |                             | |            |
    |            |  |                             | |            |
    |            |  |                             | |            |
    |            |  |                             | |            |
    |            |  |                             | |            |
    +------------+  +-----------------------------+ +------------+
    
    

    我不明白造成这种情况的原因。有人可以指出我可能犯的错误吗?

1 个答案:

答案 0 :(得分:1)

您希望实现水平回收视图,重复这三个框。但是,将这3个盒子视为单独的回收器项目的方法将不允许您在组中滚动它们。你要做的是定义一个单独的布局,其中包含这3个框。然后,为3个项目提供数据并将其绑定在视图持有者

基本上你必须操纵你的数据,然后再显示它。

public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == GROUP_ITEM) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.group_item, parent, false);
        return new GroupViewHolder(view);
    } else if (viewType == SINGLE_ITEM) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.single_item, parent, false);
        return new SingleViewHolder(view);
    }

    return null;
}

public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof GroupViewHolder) {
        ((GroupViewHolder) holder).populateData(position);
    } else if (holder instanceof SingleViewHolder) {
      // populate your normal view.
    }
}

enter image description here