fbsamples / audience-network

Open source projects to demonstrate SDK and sample code usages and integration, and to collaborate and support peers in this community.
https://developers.facebook.com/docs/audience-network
Other
319 stars 370 forks source link

Repetition of Ads in RecyclerView on LoadMore #90

Closed charlizesmith closed 5 years ago

charlizesmith commented 5 years ago

I am implementing FbNativeAds in Recyclerview and have took the code reference from the Audience Network Android code samples on Github. I am able to load the ads at the specific postion in recyclerview using nativeAdsManager.I am facing one issue that I only get 10 ads and those same ads gets repeated. I think nativeAdsManager bring the 10 Ads at first time when I call loadAds as I have given numAdsRequested=10 and once 10 different ads are loaded It repeats those 10 ads again. Is there any wayout for avoiding this repetition of ads and loading new ads?

Below is the code of my Project

//Fragment onCreate()

String placement_id ="YOUR_PLACEMENT_ID";
    nativeAdsManager = new NativeAdsManager(getActivity(),placement_id,10);
    nativeAdsManager.loadAds();        

    DataAdapter = new DataAdapter(getActivity(), new ArrayList<Object>(0),nativeAdsManager);

//Adapter

    public DataAdapter(Context mContext, List<Object> dataList, NativeAdsManager nativeAdsManager) 
    {
        this.dataList = dataList;
        this.mContext = mContext;        
        this.nativeAdsManager=nativeAdsManager;
    }

    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position, List<Object> list) {
     if (holder.getItemViewType() == AD_TYPE) {
        NativeAd nativeAd;
         nativeAd = nativeAdsManager.nextNativeAd();
        if (nativeAd != null) {

            // Set the Text.

            String advertiserName;
            if (nativeAd.getAdvertiserName().length() >= 20) {
                advertiserName = nativeAd.getAdvertiserName().substring(0, 20)+ "...";
            } else {
                advertiserName= nativeAd.getAdvertiserName();
            }

            String adBodyText;
            if (nativeAd.getAdBodyText().length() >= 72) {
                adBodyText = nativeAd.getAdBodyText().substring(0, 72)+ "...";
            } else {
                adBodyText= nativeAd.getAdBodyText();
            }

            ((AdHolder) holder).nativeAdTitle.setText(advertiserName);
            ((AdHolder) holder).nativeAdBody.setText(adBodyText);
            ((AdHolder) holder).nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
            ((AdHolder) holder).sponsoredLabel.setText(nativeAd.getSponsoredTranslation());
            ((AdHolder) holder).nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
            ((AdHolder) holder).nativeAdCallToAction.setVisibility(nativeAd.hasCallToAction() ? View.VISIBLE : View.INVISIBLE);

            AdChoicesView adChoicesView = new AdChoicesView(mContext, nativeAd, true);
            ((AdHolder) holder).adChoicesContainer.removeAllViews();
            ((AdHolder) holder).adChoicesContainer.addView(adChoicesView, 0);

            List<View> clickableViews = new ArrayList<>();
            clickableViews.add(((AdHolder) holder).nativeAdIcon);
            clickableViews.add(((AdHolder) holder).nativeAdMedia);
            clickableViews.add(((AdHolder) holder).nativeAdCallToAction);

            nativeAd.registerViewForInteraction(
                    ((AdHolder) holder).itemView,
                    ((AdHolder) holder).nativeAdMedia,
                    ((AdHolder) holder).nativeAdIcon,
                    clickableViews);

        }
    }
}
phzhou commented 5 years ago

Hi @charlizesmith thanks for the sample code. Yes the NativeAdsManager only provides the functionality of loading numAdsRequested ads in one go. In order to fulfill the usage you described, you would have to implement your own class to store the loaded ads (maybe using NativeAdsManager to load ads in batches), and find the right place to insert them into your RecyclerView. Hope this answers your question. Thanks!