RycyclerView android中的Facebook原生广告 [英] Facebook Native Ads in RycyclerView android

查看:281
本文介绍了RycyclerView android中的Facebook原生广告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了类似的问题在回收者视图android中的Facebook原生广告 ,但在与自定义广告集成方面存在一些问题.

I found similar issue Facebook Native ads in recycler view android , but had some problems with integration with Custom Ad.

我第一次尝试描述NativeAdsManager:

  @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    manager = new NativeAdsManager(getActivity(),    "892769720837476_892772047503910", 5);
    manager.setListener(this);
    manager.loadAds();

    nativeAd = new NativeAd(getActivity(), "892769720837476_892772047503910");
    nativeAd.setAdListener(this);
    nativeAd.loadAd();
...
 }

然后我将Native ad作为参数包含在RecyclerAdapter构造函数中:

Then I included Native ad as a parameter in RecyclerAdapter constructor:

  adapter = new RecyclerAdapter(foodDataList, getActivity(), nativeAd);

在此Class中,我还实现了AdListenerNativeAdsManager.Listener方法:

In this Class I also implement AdListener and NativeAdsManager.Listener methods:

@Override
public void onError(Ad ad, AdError adError) {

}

@Override
public void onAdLoaded(Ad ad) {
}

@Override
public void onAdClicked(Ad ad) {

}

@Override
public void onAdsLoaded() {

    System.out.println("Loaded in fragment");
    nativeAd = manager.nextNativeAd();
    nativeAd.setAdListener(this);

    adapter.notifyDataSetChanged();


}

@Override
public void onAdError(AdError adError) {

}

之后,在RecyclerAdapter类中:

   public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
    switch (holder.getItemViewType()) {
    ...
    case 2:
            AdditionalHolder new_holder = (AdditionalHolder) holder;
            adView = NativeAdView.render(context, nativeAd, NativeAdView.Type.HEIGHT_100);
            new_holder.templateContainer.addView(adView);
            return;

   ...
    public class AdditionalHolder extends RecyclerView.ViewHolder {
    protected LinearLayout templateContainer;

    public AdditionalHolder(View view) {
        super(view);

        templateContainer = (LinearLayout) view.findViewById(R.id.ad_test2);



    }
}

毕竟,我的FragmentRecyclerView变得非常笨拙",每次我看到越来越多的广告项目(1-从头开始,2-在10个项目之后,3-在接下来的10个项目之后,以及等等).

After all of that my Fragment with RecyclerView becomes very "luggy" and every time I can see more and more ads items (1 - from the beginning, 2 - after 10 items, 3 - after next 10 and so on).

这是我第一次使用多个RecyclerView holders,发现这个问题很困难.

It's my first time of using multiple RecyclerView holders, and find this issue difficult.

为您提供2个Classes

https://gist.github.com/burnix/6af8196ee8acf5c8f94e -RecyclerAdapter.class https://gist.github.com/burnix/53dc2ed7446969b78f07 -FragmentList.class

请帮助我解决接线片问题,并按需要放置AudienceNetwork.

Help me please to solve lugs problem and to place AudienceNetwork as it needs to be.

提前谢谢!

推荐答案

几个小时后,我解决了我的问题. 我需要更改RecyclerView的构造函数:

After several hours of headache I solved my problem. I needed to change the constructor of RecyclerView:

  public RecyclerAdapter(List<FoodData> food, Context context, NativeAd  nativeAd, NativeAdsManager manager) {
    this.foodDataList = food;
    this.context = context;
    this.nativeAd = nativeAd;
    this.manager = manager;
}

并在onBindViewHolder中实现原生广告的行为:

And implement the behavior of Native Ads in onBindViewHolder:

  AdditionalHolder new_holder = (AdditionalHolder) holder;
            try {
                new_holder.templateContainer.removeViewInLayout(adView);
            } catch (Exception e) {
                e.printStackTrace();
            }
            nativeAd = manager.nextNativeAd();
            try {
                adView = NativeAdView.render(context, nativeAd, NativeAdView.Type.HEIGHT_300);
            } catch (NullPointerException e) {
                e.printStackTrace();
            }

            new_holder.templateContainer.addView(adView);
            new_holder.blank_holder.setVisibility(View.GONE);

但是它并不能完全解决我的问题(滚动时出现微小滞后),但我希望它仍然对某些人有用.

But it hasn't solved my problem entirely (micro-lags while scrolling), but still it could be useful for someone, I hoped.

这篇关于RycyclerView android中的Facebook原生广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆