RycyclerView android 中的 Facebook 原生广告
Posted
技术标签:
【中文标题】RycyclerView android 中的 Facebook 原生广告【英文标题】:Facebook Native Ads in RycyclerView android 【发布时间】:2016-01-26 23:07:24 【问题描述】:我发现了类似的问题Facebook Native ads in recycler view android,但在与自定义广告集成时遇到了一些问题。
首先我试图描述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();
...
然后我将Native ad
作为参数包含在RecyclerAdapter
构造函数中:
adapter = new RecyclerAdapter(foodDataList, getActivity(), nativeAd);
在这个Class
中我还实现了AdListener
和NativeAdsManager.Listener
方法:
@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)
之后在RecyclerAdapter
类:
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);
毕竟我的Fragment
和RecyclerView
变得非常“笨拙”,每次我都能看到越来越多的广告项目(1 - 从一开始,2 - 在 10 个项目之后,3 - 在接下来的 10 个项目之后等等)。
这是我第一次使用多个RecyclerView
holders
,觉得这个问题很难。
为您提供2Classes
的要点
https://gist.github.com/burnix/6af8196ee8acf5c8f94e - RecyclerAdapter.class
https://gist.github.com/burnix/53dc2ed7446969b78f07 - FragmentList.class
请帮我解决凸耳问题并根据需要放置AudienceNetwork
。
提前致谢!
【问题讨论】:
查看这个项目:gist.github.com/burnix/84581959a9c3ebceecf8 【参考方案1】:经过几个小时的头痛后,我解决了我的问题。
我需要更改RecyclerView
的构造函数:
public RecyclerAdapter(List<FoodData> food, Context context, NativeAd nativeAd, NativeAdsManager manager)
this.foodDataList = food;
this.context = context;
this.nativeAd = nativeAd;
this.manager = manager;
并在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);
但它并没有完全解决我的问题(滚动时出现微滞后),但我希望它仍然对某人有用。
【讨论】:
您是从主要活动中的 onAdsLoaded 中删除此行nativeAd = manager.nextNativeAd();
还是保留它?【参考方案2】:
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);
【讨论】:
以上是关于RycyclerView android 中的 Facebook 原生广告的主要内容,如果未能解决你的问题,请参考以下文章