0

How to display Ads banner after every 10 records in listview ?

I could set ads banner in list view item but I can't manage listview item position. When i click listview item, every time I got next item position. for ex. I am loading 20 records in listview and after every 10 records, Ads. banner should be displayed.

Mahesh Vayak
  • 1,056
  • 12
  • 25
  • 1
    you may need to use a custom adapter and hack into methods like getView which accepts row number and returns that view, where you have to do your own math to get your ads in 8th / 10th item without missing those items, i.e shift / manage them acordiingly – Varun Garg Aug 08 '17 at 07:08
  • 1
    :) Thank you for answering my concerns – Mahesh Vayak Aug 08 '17 at 08:54
  • 1
    Please refer this link https://stackoverflow.com/a/27972680/8143436 – kalpesh satasiya Dec 19 '17 at 11:20

1 Answers1

0
Follow this code

public View getView(int position, View convertView, ViewGroup parent)
{
    if (position == 0)
    {
        if (convertView instanceof AdView)
        {
            return convertView;
        }
        else
        {
            AdView adView = new AdView(activity, AdSize.BANNER, ADMOB_PUBLISHER_ID);
            // Disable focus for sub-views of the AdView to avoid problems with
            // trackpad navigation of the list.
            for (int i = 0; i < adView.getChildCount(); i++)
            {
                adView.getChildAt(i).setFocusable(false);
            }
            adView.setFocusable(false);
            // Default layout params have to be converted to ListView compatible
            // params otherwise there will be a ClassCastException.
            float density = activity.getResources().getDisplayMetrics().density;
            int height = Math.round(AdSize.BANNER.getHeight() * density);
            AbsListView.LayoutParams params
                = new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT,
                                               height);
            adView.setLayoutParams(params);
            adView.loadAd(new AdRequest());
            return adView;
        }
    }
    else
    {
        return delegate.getView(position - 1, convertView, parent);
    }
}

@Override
public int getViewTypeCount()
{
    return delegate.getViewTypeCount() + 1;
}

@Override
public int getItemViewType(int position)
{
    return position == 0 ? delegate.getViewTypeCount()
                         : delegate.getItemViewType(position - 1);
}
kalpesh satasiya
  • 799
  • 8
  • 18