gpt4 book ai didi

android - 如何仅在 ListView 的第一个位置插入横幅广告?

转载 作者:行者123 更新时间:2023-11-30 02:52:33 27 4
gpt4 key购买 nike

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

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

public AllTopicsAdapter(上下文上下文,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 <--------像这样

最佳答案

想一想。

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

}

关于android - 如何仅在 ListView 的第一个位置插入横幅广告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23889282/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com