Android: how to implement grid with different columns/rows size

后端 未结 1 1212
南方客
南方客 2021-01-06 09:42

I need to implement something like this:

Seems like StaggeredGridLayoutManager | GridLayoutManager can not do it, how to achieve it for now?

GridVie

1条回答
  •  情书的邮戳
    2021-01-06 10:11

    You can achieve this kind of RecyclerView behavior with twoway-view

    I tested the 1.0.0-SNAPSHOT version of this lib.

    It's kind of old project (not maintened anymore), but it can help you to write your own RecyclerView/LayoutManager.

    Example of your layout

    In RecyclerViewAdapter you need to set the span logic:

    @Override
     public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            super.onBindViewHolder(holder, position);
            final SpannableGridLayoutManager.LayoutParams lp =
                    (SpannableGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
    
            final int span1;
            final int span2;
            if (position == 0) {
                span1 = 2;
                span2 = 2;
            } else if (position == 3) {
                span1 = 2;
                span2 = 3;
            } else {
                span1 = 1;
                span2 = 1;
            }
    
            final int colSpan = (span2);
            final int rowSpan = (span1);
    
            if (lp.rowSpan != rowSpan || lp.colSpan != colSpan) {
                lp.rowSpan = rowSpan;
                lp.colSpan = colSpan;
                holder.itemView.setLayoutParams(lp);
            }
    }
    

    I used custom RecyclerView from the library (TwoWayView):

    
    

    Result screenshot:

    0 讨论(0)
提交回复
热议问题