Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- NativeAdsManager listNativeAdsManager;
- @Override
- protected void onStart() {
- super.onStart();
- // Initialize the Audience Network SDK
- AudienceNetworkAds.initialize(this);
- ... // interstitialAdFromFacebook works fine
- // Native Facebook ads
- listNativeAdsManager = new NativeAdsManager(this,fb_native,10);
- listNativeAdsManager.loadAds(NativeAd.MediaCacheFlag.ALL);
- }
- NativeAd loadAndShowNativeAd(){
- //calling this method from adapter Class.
- try {
- return listNativeAdsManager.nextNativeAd();
- } catch (Exception E){
- return null;
- }
- }
- void reloadNativeAds(){
- //this is also called from adapter class.
- try {
- listNativeAdsManager.loadAds(NativeAd.MediaCacheFlag.ALL);
- } catch (Exception E){
- }
- }
- @Override
- public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
- ...
- // loading ad on every 7th recyclerview Item
- if (position %7 == 0 && position !=0){
- myHolder.ad_unit.setVisibility(View.VISIBLE);
- try{
- NativeAd nativeAd = ((MainActivity)mContext).loadAndShowNativeAd();
- if (nativeAd != null && nativeAd.isAdLoaded() && !nativeAd.isAdInvalidated()) {
- //show ad
- nativeAd.unregisterView();
- // Add the Ad view into the ad container.
- NativeAdLayout nativeAdLayout = myHolder.ad_unit;
- LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- // Inflate the Ad view. The layout referenced should be the one you created in the last step.
- LinearLayout adView = (LinearLayout) inflater.inflate(R.layout.native_ad_layout, nativeAdLayout, false);
- nativeAdLayout.addView(adView);
- // Add the AdOptionsView
- LinearLayout adChoicesContainer = adView.findViewById(R.id.ad_choices_container);
- AdOptionsView adOptionsView = new AdOptionsView(((MainActivity)mContext), nativeAd, nativeAdLayout);
- adChoicesContainer.removeAllViews();
- adChoicesContainer.addView(adOptionsView, 0);
- // Create native UI using the ad metadata.
- AdIconView nativeAdIcon = adView.findViewById(R.id.native_ad_icon);
- TextView nativeAdTitle = adView.findViewById(R.id.native_ad_title);
- MediaView nativeAdMedia = adView.findViewById(R.id.native_ad_media);
- TextView nativeAdSocialContext = adView.findViewById(R.id.native_ad_social_context);
- TextView nativeAdBody = adView.findViewById(R.id.native_ad_body);
- TextView sponsoredLabel = adView.findViewById(R.id.native_ad_sponsored_label);
- Button nativeAdCallToAction = adView.findViewById(R.id.native_ad_call_to_action);
- // Set the Text.
- nativeAdTitle.setText(nativeAd.getAdvertiserName());
- nativeAdBody.setText(nativeAd.getAdBodyText());
- nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
- nativeAdCallToAction.setVisibility(nativeAd.hasCallToAction() ? View.VISIBLE : View.INVISIBLE);
- nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
- sponsoredLabel.setText(nativeAd.getSponsoredTranslation());
- // Create a list of clickable views
- List<View> clickableViews = new ArrayList<>();
- clickableViews.add(nativeAdTitle);
- clickableViews.add(nativeAdCallToAction);
- // Register the Title and CTA button to listen for clicks.
- nativeAd.registerViewForInteraction(
- adView,
- nativeAdMedia,
- nativeAdIcon,
- clickableViews);
- } else if (nativeAd.isAdInvalidated()){
- ((MainActivity)mContext).reloadNativeAds();
- Toast.makeText((MainActivity)mContext, "nativeAd.isAdLoaded() : "+nativeAd.isAdLoaded()+" isAdInvalidated()", Toast.LENGTH_SHORT).show();
- }
- } catch (Exception e){
- }
- } else {
- myHolder.ad_unit.removeAllViewsInLayout();
- myHolder.ad_unit.setVisibility(View.GONE);
- }
- }//onBindViewHolder
Add Comment
Please, Sign In to add comment