Facebook Native Ads in RycyclerView android

前端 未结 2 569
渐次进展
渐次进展 2021-02-06 13:58

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 Nativ

相关标签:
2条回答
  • 2021-02-06 14:15

    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.

    0 讨论(0)
  • 2021-02-06 14:36
           Youur adapter==>
    
            import android.app.Activity;
            import android.support.v7.widget.CardView;
            import android.support.v7.widget.RecyclerView;
            import android.util.Log;
            import android.view.LayoutInflater;
            import android.view.View;
            import android.view.ViewGroup;
            import android.widget.ImageView;
            import android.widget.LinearLayout;
            import android.widget.ProgressBar;
    
            import com.bumptech.glide.Glide;
            import com.bumptech.glide.load.resource.drawable.GlideDrawable;
            import com.bumptech.glide.request.RequestListener;
            import com.bumptech.glide.request.target.Target;
            import com.romantic.pictures.hd.love.wallpaper.download.R;
            import com.romantic.pictures.hd.love.wallpaper.download.activity.ROMANTIC_LOVE_WALLPAPER;
            import com.romantic.pictures.hd.love.wallpaper.download.model.ImageStorage;
    
            import java.util.ArrayList;
    
            /**
             * Created by abc on 4/11/2018.
             */
    
            public class ImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    
                public final int CONTENT_TYPE = 0;
                public final int AD_TYPE = 1;
    
                private OnItemClickListener mOnItemClickListener;
                Activity context;
                ArrayList<ImageStorage> catagorilist;
    
                public ImageAdapter(Activity context, ArrayList<ImageStorage> catagorilist, OnItemClickListener mOnItemClickListener) {
                    this.mOnItemClickListener = mOnItemClickListener;
                    this.context = context;
                    this.catagorilist = catagorilist;
                }
    
    
                public interface OnItemClickListener {
                    public void onItemClick(View view, int position);
                }
    
                @Override
                public int getItemViewType(int position) {
                    Log.e("TAG", "getItemViewType position : " + position);
                    Log.e("TAG", "getItemViewType position% : " + position % 5);
    
                    if (position % 7 == 0)
                        return AD_TYPE;
                    return CONTENT_TYPE;
                }
    
    
                @Override
                public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                    switch (viewType) {
                        case CONTENT_TYPE:
                            Log.e("TAG", "content type : ");
                            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_layout2, parent, false);
                            return new MyHolder(view);
    
                        case AD_TYPE:
                            Log.e("TAG", "ad type : ");
                            View nativeView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_nativead, parent, false);
                            return new NativeAd(nativeView);
                        default:
                            Log.e("TAG", "default : ");
                            LayoutInflater inflater1 = LayoutInflater.from(parent.getContext());
                            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_layout2, parent, false);
                            return new MyHolder(v);
                    }
                }
    
    
                @Override
                public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
                    holder.setIsRecyclable(false);
                    int viewType = getItemViewType(position);
                    ImageStorage listitem = catagorilist.get(position);
    
    
                    switch (viewType) {
                        case CONTENT_TYPE:
                            Log.e("TAG", "bind CONTENT_TYPE : ");
                            final MyHolder menuItemHolder = (MyHolder) holder;
    
    
                            //        holder.imagview.setText(listitem.getId());
                    /*holder.cat_name.setText(listitem.getName());*/
                            Glide.with(context).load("http://learntodraw.in/ImageApp/new_" + listitem.getCat_imagem()).listener(new RequestListener<String, GlideDrawable>() {
                                @Override
                                public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                                    return false;
                                }
    
                                @Override
                                public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                                    menuItemHolder.progressBar.setVisibility(View.GONE);
                                    return false;
                                }
                            }).into(menuItemHolder.imagview);
                            Log.e("TAG", "onBindViewHolder: " + "http://learntodraw.in/ImageApp/new_" + listitem.getCat_imagem());
                            Log.e("TAG", "onBindViewHolder: " + position);
    
    
                            menuItemHolder.mainLayout.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    mOnItemClickListener.onItemClick(v, position);
                                }
                            });
    
    
                            break;
    
                        case AD_TYPE:
                            Log.e("TAG", "bind AD_TYPE : ");
                            final NativeAd nativeHolder = (NativeAd) holder;
                            ROMANTIC_LOVE_WALLPAPER.showNativeVidAd(context, nativeHolder.container);
    
                            nativeHolder.container_height.post(new Runnable() {
                                public void run() {
            //                        int height = nativeHolder.container_height.getHeight();
                                    ViewGroup.LayoutParams params = nativeHolder.container.getLayoutParams();
            //                        params.height = height;
                                    params.height = LinearLayout.LayoutParams.WRAP_CONTENT;
                                    nativeHolder.container.setLayoutParams(params);
    
                                  //  nativeHolder.linearlayout_title.setVisibility(View.GONE);
                                }
                            });
                            break;
                    }
    
    
                }
    
                @Override
                public int getItemCount() {
                    return catagorilist.size();
                }
    
    
                public class MyHolder extends RecyclerView.ViewHolder {
                    public ImageView imagview;
                    public LinearLayout mainLayout;
                    public CardView cardview;
                    public ProgressBar progressBar;
    
                    public MyHolder(View itemView) {
                        super(itemView);
    
    
                        imagview = (ImageView) itemView.findViewById(R.id.imagview);
                        mainLayout = (LinearLayout) itemView.findViewById(R.id.mainLayout);
                        progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar);
                        cardview = (CardView) itemView.findViewById(R.id.cardview);
                    }
                }
    
                public static class NativeAd extends RecyclerView.ViewHolder {
    
                    LinearLayout container;
                    LinearLayout container_height;
            //        LinearLayout linearlayout_title;
    
                    public NativeAd(View view) {
                        super(view);
                        container = view.findViewById(R.id.native_ad_container);
                        container_height = view.findViewById(R.id.container_height);
                       /* linearlayout_title = view.findViewById(R.id.linearlayout_title);*/
                    }
                }
    
            }
    
    
    
    
    
    
    public static void showNativeVidAd(final Activity context, final LinearLayout nativeContainer) {
    
            final NativeAd  nativeAd = new NativeAd(context, context.getString(R.string.fb_test_ad_img) + context.getString(R.string.native_ads3));
    //        final NativeAd nativeAd = new NativeAd(context, context.getString(R.string.facebook_native_ad_unit_id));
            nativeAd.setAdListener(new com.facebook.ads.AdListener() {
    
                @Override
                public void onError(Ad ad, AdError error) {
                    // Ad error callback
                    Log.e("TAG", "facebook native error : " + error.getErrorMessage());
                    nativeContainer.setVisibility(View.GONE);
                }
    
                @Override
                public void onAdLoaded(Ad ad) {
                    // Ad loaded callback
                    Log.e("TAG", "facebook native onAdLoaded : ");
                    if (nativeAd != null) {
                        nativeAd.unregisterView();
                    }
    
                    try {
                        LinearLayout nativeAdContainer = nativeContainer;
    
                        nativeAdContainer.setVisibility(View.VISIBLE);
                        // Add the Ad view into the ad container.
                        nativeAdContainer = (LinearLayout) context.findViewById(R.id.native_ad_container);
                        LayoutInflater inflater = LayoutInflater.from(context);
                        // Inflate the Ad view.  The layout referenced should be the one you created in the last step.
                        RelativeLayout adView = (RelativeLayout) inflater.inflate(R.layout.custom_nativead, nativeAdContainer, false);
    
                        nativeAdContainer.removeAllViews();
                        nativeAdContainer.addView(adView);
    
                        // Create native UI using the ad metadata.
                        MediaView nativeAdMedia = (MediaView) adView.findViewById(R.id.native_ad_media);
    
                      /*  //TextView nativeAdSocialContext = (TextView) adView.findViewById(R.id.native_ad_social_context);
                        Typeface typeface = Typeface.createFromAsset(context.getAssets(), "ProximaNova-Semibold.otf");
                        nativeAdSocialContext.setTypeface(typeface);*/
    
    //                    TextView nativeAdCallToAction = (TextView) adView.findViewById(R.id.native_ad_call_to_action);
    
                        // Set the Text.
    //                    nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
    //                    nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
    
                        // Download and display the ad icon.
                       // NativeAd.Image adIcon = nativeAd.getAdIcon();
    
                        // Download and display the cover image.
                        nativeAdMedia.setNativeAd(nativeAd);
    
                        // Add the AdChoices icon
                        LinearLayout adChoicesContainer = (LinearLayout) adView.findViewById(R.id.ad_choices_container);
                        AdChoicesView adChoicesView = new AdChoicesView(context, nativeAd, true);
                        adChoicesContainer.addView(adChoicesView);
    
                        // Register the Title and CTA button to listen for clicks.
                        List<View> clickableViews = new ArrayList<>();
    //                    clickableViews.add(nativeAdCallToAction);
                        clickableViews.add(nativeAdMedia);
                        clickableViews.add(adChoicesContainer);
                        nativeAd.registerViewForInteraction(nativeAdContainer, clickableViews);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
    
                @Override
                public void onAdClicked(Ad ad) {
                    // Ad clicked callback
                }
    
                @Override
                public void onLoggingImpression(Ad ad) {
                    // Ad impression logged callback
                    Log.e("TAG", "facebook native onLoggingImpression");
                }
            });
    
            // Request an ad
            nativeAd.loadAd(NativeAd.MediaCacheFlag.ALL);
        }
    
    0 讨论(0)
提交回复
热议问题