Facebook 原生横幅广告在 ListView 中显示重复布局

Facebook Native banner ads displaying duplicate layout in ListView

我正在 ListView 中实施 Facebook 原生横幅广告。广告工作正常,但广告上方也显示了广告的空布局。

下面是我的 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,但它会在运行时产生错误。

这是截图

在添加新视图之前您需要从现有视图中删除以前添加的视图,这可能会导致重复视图 nativeBannerAdContainer.removeAllViews(); // 在添加新视图之前添加这行代码 nativeBannerAdContainer.addView(adView); //

如果您删除 removeAllViews,这将删除之前添加的所有视图,并且您的视图永远不会被复制

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