我正在ListView
中实施Facebook Native横幅广告。广告运作正常,但广告上方的空白布局也显示在广告上方。
下面是我的ListView
的适配器代码:
public CouponsViewAdapter(Context context, List<DataSetForCoupons>
couponsdata, String amount_value, String id, String value,
List<DataSetForAddCoupons> addcouponsdata, boolean saving) {
this.context = context;
this.DataList = couponsdata;
this.DataListAdd = addcouponsdata;
this.amount = amount_value;
this.id = id;
this.value = value;
this.saving = saving;
for (int i = 0; i < DataList.size(); i++) {
if (i % 2 == 0 && i != 0) {
itemType.add(i, 1);
DataList.add(i, null);
} else
itemType.add(i, 0);
}
}
@Override
public int getCount() {
return DataList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
if (itemType.get(position) == 1)
return SECOND_ITEM;
else
return FIRST_ITEM;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
if (convertView == null) {
final LayoutInflater Inflater = (LayoutInflater) context.getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
switch (type) {
case SECOND_ITEM:
nativeBannerAd = new NativeBannerAd(context, "IMG_16_9_APP_INSTALL#635165165156968704093");
convertView = Inflater.inflate(R.layout.banner_ad, null, false);
nativeBannerAdContainer = convertView.findViewById(R.id.banner_ad_layout);
nativeBannerAd.setAdListener(new NativeAdListener() {
@Override
public void onAdLoaded(Ad ad) {
if (nativeBannerAd == null || nativeBannerAd != ad) {
return;
}
nativeBannerAd.unregisterView();
adView = (LinearLayout) Inflater.inflate(R.layout.banner_ad,
nativeBannerAdContainer, false);
nativeBannerAdContainer.addView(adView);
RelativeLayout adChoicesContainer = adView.findViewById(R.id.ad_choices_container);
AdChoicesView adChoicesView = new AdChoicesView(context, nativeBannerAd, true);
adChoicesContainer.addView(adChoicesView, 0);
AdIconView nativeAdIconView = adView.findViewById(R.id.native_icon_view);
Button nativeAdCallToAction = adView.findViewById(R.id.native_ad_call_to_action);
List<View> clickableViews = new ArrayList<>();
clickableViews.add(nativeAdTitle);
clickableViews.add(nativeAdCallToAction);
nativeBannerAd.registerViewForInteraction(adView, nativeAdIconView, clickableViews);
}
@Override
public void onAdClicked(Ad ad) {
}
@Override
public void onLoggingImpression(Ad ad) {
}
});
nativeBannerAd.loadAd();
break;
case FIRST_ITEM:
//Implementing the ListView here
break;
return convertView;
}
我尝试使用convertView
代替adView
,但是在运行时会产生错误。
这是屏幕截图
答案 0 :(得分:1)
添加新视图之前,您需要从现有视图中删除以前添加的视图,这都不会导致重复视图 nativeBannerAdContainer.removeAllViews(); //在添加新视图之前添加此行代码 nativeBannerAdContainer.addView(adView); //
nativeBannerAd.setAdListener(new NativeAdListener() {
@Override
public void onAdLoaded(Ad ad) {
if (nativeBannerAd == null || nativeBannerAd != ad) {
return;
}
nativeBannerAd.unregisterView();
adView = (LinearLayout) Inflater.inflate(R.layout.banner_ad,
nativeBannerAdContainer, false);
nativeBannerAdContainer.removeAllViews();
nativeBannerAdContainer.addView(adView);
RelativeLayout adChoicesContainer = adView.findViewById(R.id.ad_choices_container);
AdChoicesView adChoicesView = new AdChoicesView(context, nativeBannerAd, true);
adChoicesContainer.addView(adChoicesView, 0);
AdIconView nativeAdIconView = adView.findViewById(R.id.native_icon_view);
Button nativeAdCallToAction = adView.findViewById(R.id.native_ad_call_to_action);
List<View> clickableViews = new ArrayList<>();
clickableViews.add(nativeAdTitle);
clickableViews.add(nativeAdCallToAction);
nativeBannerAd.registerViewForInteraction(adView, nativeAdIconView, clickableViews);
}
@Override
public void onAdClicked(Ad ad) {
}
@Override
public void onLoggingImpression(Ad ad) {
}
});