如何在listview中显示广告横幅时管理商品位置

时间:2017-08-08 07:01:53

标签: android listview

如何在listview中每10条记录后显示广告横幅?

我可以在列表视图项目中设置广告横幅,但我无法管理列表视图项目位置。 当我点击listview项目时,每次我得到下一个项目位置。 对于前 我在listview中加载了20条记录,每10条记录后加载广告。应显示横幅。

1 个答案:

答案 0 :(得分: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);
}