如何仅在列表视图的第一个位置插入横幅广告?

时间:2014-05-27 12:19:49

标签: android android-listview admob

http://googleadsdeveloper.blogspot.in/2012/03/embedding-admob-ads-within-listview-on.html引用此链接在第n个位置插入广告。但我现在只想在第一个位置插入

=============================================== ================================== 下面的代码是插入第n个位置[工作正常]

public AllTopicsAdapter(Context context,BaseAdapter delegate){

    this.context = context;
    this.delegate = delegate;
}

@Override
public int getCount() {
    // Total count includes list items and ads.
    return delegate.getCount()+2;
}

@Override
public Object getItem(int position) {
    // Return null if an item is an ad. Otherwise return the delegate item.
    if (isItemAnAd(position)) {
        return null;
    }
    return delegate.getItem(getOffsetPosition(position));
}

@Override
public boolean areAllItemsEnabled() {
    return false;
}

@Override
public boolean isEnabled(int position) {
    return (!isItemAnAd(position))
            && delegate.isEnabled(getOffsetPosition(position));
}

@Override
public long getItemId(int position) {
    return position;
}

private boolean isItemAnAd(int position) {
    // Place an ad at the first and last list view positions.
    return (position == 0 || position == (getCount() - 1));
}

private int getOffsetPosition(int position) {
    return position - 1;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    int k = 2;
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if ((position % k) == 0) {

        convertView = inflater.inflate(R.layout.adview, null);
        // set ad content
        convertView.setBackground(null);
        moPubView = (MoPubView) convertView.findViewById(R.id.adview);

        moPubView.setAdUnitId("xxxxxxxxxxxxxxxxxxxxxxx");

        moPubView.setBannerAdListener(this);
        moPubView.loadAd();
        return convertView;
    }

    else {

        return delegate.getView(position - (int) Math.ceil(position / k)- 1, convertView, parent);
    }
}

=============================================== ========================================== //尝试在第一个位置插入广告。但在这种情况下,它正在替换我的项目。

                   if (position== 1) {

        convertView = inflater.inflate(R.layout.adview, null);
        // set ad content
        convertView.setBackground(null);
        moPubView = (MoPubView) convertView.findViewById(R.id.adview);

        moPubView.setAdUnitId("b2bd1dff9963441aaa1b9603bcf6d5d3");

        moPubView.setBannerAdListener(this);
        moPubView.loadAd();
        return convertView;
    }

    else {


        return delegate.getView(position, convertView, parent);
    }

0,ad,1,2,3,4,5,6,7,8,9,10,11,12< ---------喜欢这个

3 个答案:

答案 0 :(得分:1)

做一个想法。

@Override
        public int getCount()
        {

            return item.size()+1;
        }

并在

@覆盖

    public View getView(final int position, View convertView, ViewGroup parentView)
{

    if (position == 0)
    {
        // ad
    } else if(position>0)
    {
        item.get(position - 1);// access array by deducting -1 .
    }

}

答案 1 :(得分:0)

您要找的是ListView的{​​{3}}。它允许您提供View作为标题(它将插入ListView的{​​{1}}的第1个位置)。

答案 2 :(得分:0)

试试希望它有效

View header = getLayoutInflater().inflate(R.layout.header, null);
ListView listView = getListView();
listView.addHeaderView(header);