Search code examples
androidandroid-recyclerviewadmobgridlayoutmanager

Admob issue when using GridLayoutManager with RecyclerView


I have a recyclerview with GridLayoutManager and have its spancount as 3. I have two viewtypes - one for the recyclerview items and one for ads(admob). Following is the result when my spancount is 3

enter image description here

You can see that the ad is not visible, now when i change the spancount to 1 it becomes visible like below

enter image description here

i think this is because the admob ads don't support these dimensions, is there a work around for this?

the ad is not showing up even if i try to hardcode the adsize My question is that is there a way to fit the adview into my recyclerview with GridLayoutManager and spancount as 3


Solution

  • Yes, this is possible. All you have to do is to call setSpanSizeLookup on your layout manager like below

    mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            if (position % MainActivity.ITEMS_PER_AD == 0) {//your condition for showing ad
                return 3;//replace 3 with the number of items in each row
            }
            return 1;
        }
    });
    

    And then set the layout manager to your recyclerview.