Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show banner ads intermittently in gridview

I am developing an android app that will have a screen similar to the following image - enter image description here

Please notice the banner ad between the cells. As GridView does not support such spanning of columns I am at a loss as to what to do.

Please provide any suggestions you can. Thanks in advance.

like image 717
Pankaj Avatar asked Nov 10 '14 10:11

Pankaj


2 Answers

On my opinion, the best way to do it is to use RecyclerView with GridLayoutManager. Simply provide SpanSizeLookup for GridLayoutManager that can determine size of each position. For example, first position need to ocupate full row

//Two items in a row
int spanCount = 2;
GridLayoutManager manager = new GridLayoutManager(getActivity(),spanCount);
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return (position == 0 ? spanCount : 1);
            }
        });

In your adapter provide 2 types of items - usual item and advertisement item

It is preferable to set item paddings via ItemDecoration.

I can provide more code if needed

like image 99
Dmytro Avatar answered Oct 28 '22 05:10

Dmytro


You need to create two layouts for items in Gridview Adapter. One for what you want to show and other for adView and alternately place layout while creating view.

Check this to get more idea.

like image 22
virendrao Avatar answered Oct 28 '22 07:10

virendrao