0

I have RecyclerView, where every 14-th item is a Facebook Audience Network Ad.

@Override
public int getItemViewType(int position) {
    int viewType = 0;
    if (position % 14 == 0 && position != 0) viewType = 2;
    return viewType;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    switch (viewType) {
        case 0:
            return  new MainViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false));
        case 2:
            return new AdHolder((LayoutInflater.from(parent.getContext()).inflate(R.layout.ad_test3, parent, false)));
    }
    return null;
}

The is as follows: every 14th element is the same. Here's onBindViewHolder method.

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
    final FoodData foodData = foodDataList.get(position);

    switch (holder.getItemViewType()) {
        case 0:
            MainViewHolder mainViewHolder = (MainViewHolder) holder;
            ...
            break;
        case 2:
            AdHolder adHolder = (AdHolder) holder;
            //System.out.println("ad hasH" + position);
            if (manager.isLoaded()) {
                NativeAd nativeAd;
                if (map.containsKey(position)) {
                    nativeAd = map.get(position);
                } else {
                    nativeAd = manager.nextNativeAd();
                    map.put(position, nativeAd);
                }

                System.out.println(" Native Ad" + nativeAd.hashCode());
                System.out.println(" Native Ad.Title" + nativeAd.getAdTitle());

                adHolder.templateContainer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Config.AD_HEIGHT_DP));
                adHolder.nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
                adHolder.nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
                adHolder.nativeAdTitle.setText(nativeAd.getAdTitle());
                adHolder.nativeAdBody.setText(nativeAd.getAdBody());

                Picasso.with(context)
                        .load(nativeAd.getAdIcon().getUrl())
                        .tag("resume_tag")
                        .into(adHolder.nativeAdIcon);

                Picasso.with(context)
                        .load(nativeAd.getAdCoverImage().getUrl())
                        .resize(width, ad_height)
                        .tag("resume_tag")
                        .placeholder(R.drawable.adholder2)
                        .into(adHolder.nativeAdMedia);

                System.out.println("url =" + nativeAd.getAdCoverImage().getUrl());

                if (adHolder.adChoicesView == null) {
                    adHolder.adChoicesView = new AdChoicesView(context, nativeAd, true);
                    adHolder.adChoiceContainer.addView(adHolder.adChoicesView, 0);
                }
                nativeAd.registerViewForInteraction(holder.itemView);
            } else {
                adHolder.params = adHolder.templateContainer.getLayoutParams();
                adHolder.templateContainer.setLayoutParams(new ViewGroup.LayoutParams(0, 0));
            }
            break;
    }

}

What I can't understand is when I check where I the same NativeAd object:

 System.out.println(" Native Ad" + nativeAd.hashCode());
 System.out.println(" Native Ad.Title" + nativeAd.getAdTitle());

I find, that hashCode of the NativeAd changes, but title (and other elements) doesn't!

Hope somebody we'll help me. Here's full code of Adapter https://gist.github.com/burnix/c1dd34dd896f5c6ddc6b2b8971908e28

1 Answer 1

1

It would be good to post a sample project for checking the issue. Base on the adapter code only is not sufficient.

From the code, the short answer is the ad manager was not loaded properly.

The long answer is when ad manager is called with manager.laodAds(), the ads can be filled fully or partially, and checking manager.isLoaded() is not enough. The best practice is to set the NativeAdsManager.Listener::onAdsLoaded() to ensure all ads are loaded properly. It's also recommended to move manager from adapter to activity.

Notable point is each nativeAd only register once registerViewForInteraction(View). This is not correct. Since the viewHolder will be different each time, to allow the nativeAd work properly, it has to unregister the view frist then register it again for the right view.

Another notable point is the manager only requested 3 native ads from beginning, and the code inside adapter is keeping calling manager.nextNativeAd() without check the valid number. This will cause continuous loop within the 3 ads if placements are more than 3. This may not be the best way to do base on revenue consideration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.