Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在ListView中显示重复布局的Facebook本机横幅广告_Android_Android Recyclerview_Facebook Sdk 4.0_Facebook Audience Network - Fatal编程技术网

Android 在ListView中显示重复布局的Facebook本机横幅广告

Android 在ListView中显示重复布局的Facebook本机横幅广告,android,android-recyclerview,facebook-sdk-4.0,facebook-audience-network,Android,Android Recyclerview,Facebook Sdk 4.0,Facebook Audience Network,我正在我的列表视图中实现Facebook原生横幅广告。广告运行良好,但广告的空白布局也显示在广告上方 下面是我的列表视图的适配器代码: public CouponsViewAdapter(Context context, List<DataSetForCoupons> couponsdata, String amount_value, String id, String value, List<DataSetForAddCoupons> addcouponsdata,

我正在我的
列表视图中实现Facebook原生横幅广告。广告运行良好,但广告的空白布局也显示在广告上方

下面是我的
列表视图的适配器代码:

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;
}
公共耦合视图适配器(上下文,列表 couponsdata、字符串金额\u值、字符串id、字符串值、, 列表addcouponsdata,布尔值保存){ this.context=上下文; this.DataList=couponsdata; this.DataListAdd=addcouponsdata; this.amount=amount\u值; this.id=id; 这个值=值; 这个。储蓄=储蓄; 对于(int i=0;i
我尝试使用
convertView
而不是
adView
,但它在运行时产生错误

这是截图


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

如果删除将删除以前添加的所有视图的RemoveAllView,则您的视图将永远不会重复
nativeBannerAd.setAdListener(新的NativeAdListener(){
@凌驾
已加载的公共无效(Ad){
if(nativeBannerAd==null | | nativeBannerAd!=ad){
回来
}
nativeBannerAd.unregisterView();
adView=(线性布局)充气机。充气(右布局。横幅),
NativeBannerDataContainer,假);
nativeBannerAdContainer.removeAllViews();
nativeBannerAdContainer.addView(adView);
RelativeLayout adChoicesContainer=adView.findViewById(R.id.ad\u choices\u container);
AdChoicesView AdChoicesView=新的AdChoicesView(上下文,nativeBannerAd,true);
adChoicesContainer.addView(adChoicesView,0);
AdIconView nativeAdIconView=adView.findviewbyd(R.id.native\u图标\u视图);
Button nativeAddCallToAction=adView.findViewById(R.id.native\u ad\u call\u to\u action);
List clickableViews=新建ArrayList();
clickableViews.add(NativeAddTitle);
单击可查看。添加(NativeAddCallToAction);
nativeBannerAd.registerViewForInteraction(adView、nativeAdIconView、ClickableView);
}
@凌驾
已单击公共无效(Ad){
}
@凌驾
公众虚无在线印象(Ad){
}
});

show your full adapter code更新了代码。case FIRST_ITEM code很大,所以我忽略了它。谢谢,伙计。效果很好。但它在消失之前会显示一段时间。你知道如何修复它吗?你可以隐藏adView可见性,在nativeBannerAd.registerViewForInteraction之后可见。
 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) {
        }
    });