简体   繁体   中英

How to insert banner ad only at 1st position of list view?

http://googleadsdeveloper.blogspot.in/2012/03/embedding-admob-ads-within-listview-on.html refering this link for inserting ad in nth position. But i want to insert at only 1st position now

================================================================================= Below code is to insert at nth postion[working fine]

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);
    }
}

========================================================================================= //trying to insert ad in first position. But in this case it is replacing my item.

                   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 <--------- like this

do one think.

@Override
        public int getCount()
        {

            return item.size()+1;
        }

and in

@Override

    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 .
    }

}

What you are looking for is the addHeaderView() of ListView . It allows you to provide a View that will act as a header (it will be inserted at the 1st position of the ListView ) of your ListView .

try this hope it works

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM