Facebook Native Ads in RycyclerView android

Peter Parker picture Peter Parker · Jan 27, 2016 · Viewed 7.5k times · Source

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

For the first I tried to describe 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();
...
 }

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

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

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

}

After that in RecyclerAdapter class :

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



    }
}

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).

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

Provide you with gist of 2 Classes

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

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

Thanks in advance!

Answer

Peter Parker picture Peter Parker · Jan 27, 2016

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

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.