János
János

Reputation: 35070

How to make this custom "grid" layout in Android?

enter image description here

enter image description here

I need 9 square cell then a long rectangle cell then again 9 square cell. How should I set up a LinearLayoutManager to pass it to RecyclerView?

I tried two way:

LinearLayoutManager llm2 = new LinearLayoutManager(App.getContext());
llm2.setOrientation(LinearLayoutManager.VERTICAL);

But here all item comes in one separate row.

If I set it to .HORIZONTAL, then all element will be in one long row.

Tried GridLayoutManager

RecyclerView.LayoutManager glm = new GridLayoutManager(App.getContext(), 3);

But here do not know how to handle long item.

Any idea?

Upvotes: 0

Views: 622

Answers (2)

mVck
mVck

Reputation: 3030

You could create a GridLayoutManager with a span of 3 and return the size of 3 in your spanSizeLookup for your "long element":

    final int spanCount = 3;

    GridLayoutManager glm = new GridLayoutManager(this, spanCount);
    recyclerView.setLayoutManager(glm);

    glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            // You could also use adapter.getItemViewType(position) and check if
            // the type of the view is "LONG_VIEW" instead of using a modulo
            if (position % 10 == 9) {
                return spanCount;
            } else {
                return 1;
            }
        }
    });

Upvotes: 0

tyczj
tyczj

Reputation: 73798

you can use setSpanCount after every 9 items and you will get one row that is a single cell.

so basically you have to put in your array that you pass into your adapter a "empty item" that would indicate a full row because the recyclerview still needs to know that to display it

example:

array

[data,data,data,data,data,data,data,data,data,"",data,data,etc...]

Upvotes: 1

Related Questions