Jack
Jack

Reputation: 2063

GridView: Adding custom row every 20 items

I have a standard GridView displaying images. I would like for every 20 items/images that are shown, a custom layout to be shown.

Here is an good example of what I mean:

enter image description here

Would anyone know how to achieve this? any advice is appreciated. Thank you.

Upvotes: 0

Views: 91

Answers (1)

RoShan Shan
RoShan Shan

Reputation: 2954

Would anyone know how to achieve this?

I did it. You can achieve this easily by using RecyclerView and GridLayoutManager and 2 view types in RecyclerView.Adapter. You can see codes below: With type TYPE_ADS, you span colum by 2. With normal item TYPE_ITEM, you don't span colum (means value = 1)

mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
GridLayoutManager glm = new GridLayoutManager(this, 2);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                switch(mAdapter.getItemViewType(position)){
                    case MyAdapter.TYPE_ADS:
                        return 2;
                    case MyAdapter.TYPE_ITEM:
                        return 1;
                    default:
                        return -1;
                }
            }
        });

mRecyclerView.setLayoutManager(glm); 
mRecyclerView.setAdapter(mAdapter);

Upvotes: 1

Related Questions