在 recyclerview 中实现 facebook 横幅广告
Posted
技术标签:
【中文标题】在 recyclerview 中实现 facebook 横幅广告【英文标题】:Implement facebook banner ads in recyclerview 【发布时间】:2020-07-22 06:15:47 【问题描述】:我正在尝试在 recyclerview 的第三个位置实施 Facebook 横幅广告(不是原生横幅)。我创建了一个具有广告布局的ads_row.xml
。 ads_row.xml
显示正常,但广告不可见。我将在下面发布我的适配器代码。
如果有人能给我建议,我应该使用哪种方法在 recyclerview 中展示广告,我将不胜感激。
快乐编码
NewsAdapter
public class NewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener
private List<LinkedTreeMap> newsLists;
private Context context;
private LayoutInflater inflater;
AdView adView;
int AD_TYPE = 0;
int CONTENT_TYPE = 1;
public NewsAdapter(List<LinkedTreeMap> newsLists, Context context)
inflater = LayoutInflater.from(context);
this.newsLists = newsLists;
this.context = context;
public class PostViewHolder extends RecyclerView.ViewHolder
public TextView textView;
public ImageView imageView;
public TextView description;
public PostViewHolder(@NonNull View itemView)
super(itemView);
textView = (TextView) itemView.findViewById(R.id.textView);
imageView = (ImageView) itemView.findViewById(R.id.image_ml);
description = (TextView) itemView.findViewById(R.id.description);
public class AdViewHolder extends RecyclerView.ViewHolder
private LinearLayout adContainer;
public AdViewHolder(@NonNull View itemView)
super(itemView);
adContainer = (LinearLayout) itemView.findViewById(R.id.myList_fb_banner);
adContainer.addView(adView);
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
if(viewType == CONTENT_TYPE)
View v = inflater.inflate(R.layout.mylist, parent, false);
return new PostViewHolder(v);
else
View v = inflater.inflate(R.layout.ads_row, parent, false);
AudienceNetworkAds.initialize(context);
AdSettings.setIntegrationErrorMode(INTEGRATION_ERROR_CRASH_DEBUG_MODE);
adView = new com.facebook.ads.AdView(context, "IMG_16_9_APP_INSTALL#MYPLACLEMENTID", AdSize.BANNER_HEIGHT_90);
adView.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
if (adView == null)
adView.setVisibility(View.GONE);
return new AdViewHolder(v);
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position)
if (getItemViewType(position) == CONTENT_TYPE)
LinkedTreeMap newsList = newsLists.get(position);
if (holder instanceof PostViewHolder)
PostViewHolder viewholder = (PostViewHolder) holder;
viewholder.textView.setText(newsList.get("title").toString());
if (!newsList.get("image").toString().isEmpty())
viewholder.imageView.setVisibility(View.VISIBLE);
Picasso.get()
.load(newsList.get("image").toString())
.into(viewholder.imageView);
else
viewholder.imageView.setVisibility(View.GONE);
else if(getItemViewType(position) == AD_TYPE)
if (holder instanceof AdViewHolder)
AdViewHolder adViewholder = (AdViewHolder) holder;
if (adViewholder.adContainer.getParent() != null)
((ViewGroup)adViewholder.adContainer.getParent()).removeView(adViewholder.adContainer);
adViewholder.adContainer.addView(adView);
adView.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
if (adView == null)
adView.setVisibility(View.GONE);
if (position == newsLists.size()-3)
adView.setVisibility(View.VISIBLE);
if (adView != null)
adView.loadAd();
【问题讨论】:
【参考方案1】:我注意到了一些问题。
-
如果
adView
为空,您的应用程序将崩溃。如果对象为 null,则无法操作对象的属性:
if (adView == null)
adView.setVisibility(View.GONE);
您很可能希望完全删除此语句。
-
位置计算错误。您是说要在回收商视图项目的第三行加载广告。这个表达式
newsLists.size() - 3
不会给你第三行的索引:
if (position == newsLists.size()-3)
adView.setVisibility(View.VISIBLE);
if (adView != null)
adView.loadAd();
第三行的索引总是 2.索引的枚举从0开始作为第一个索引。
将这段代码更新为:
if (position == 2)
adView.loadAd();
-
没有
getItemViewType
方法覆盖。或者至少这不是问题。
我已经更新了你的适配器。您可以尝试将其复制粘贴到您的应用程序中以检查其工作方式。
public class NewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener
private List<LinkedTreeMap> newsLists;
private Context context;
private LayoutInflater inflater;
int AD_TYPE = 0;
int CONTENT_TYPE = 1;
public NewsAdapter(List<LinkedTreeMap> newsLists, Context context)
inflater = LayoutInflater.from(context);
this.newsLists = newsLists;
this.context = context;
// AudienceNetworkAds should be initialized inside of Activity, Fragment or even better Application class
AudienceNetworkAds.initialize(context);
AdSettings.setIntegrationErrorMode(INTEGRATION_ERROR_CRASH_DEBUG_MODE);
public class PostViewHolder extends RecyclerView.ViewHolder
public TextView textView;
public ImageView imageView;
public TextView description;
public PostViewHolder(@NonNull View itemView)
super(itemView);
textView = (TextView) itemView.findViewById(R.id.textView);
imageView = (ImageView) itemView.findViewById(R.id.image_ml);
description = (TextView) itemView.findViewById(R.id.description);
public class AdViewHolder extends RecyclerView.ViewHolder
private LinearLayout adContainer;
private AdView adView;
public AdViewHolder(@NonNull View itemView)
super(itemView);
adContainer = (LinearLayout) itemView.findViewById(R.id.myList_fb_banner);
adView = new com.facebook.ads.AdView(context, "IMG_16_9_APP_INSTALL#MYPLACLEMENTID", AdSize.BANNER_HEIGHT_90);
adView.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
adContainer.addView(adView);
public void loadAd()
adView.loadAd();
@Override
public int getItemViewType(int position)
// Ad only at the third row
return position == 2 ? AD_TYPE : CONTENT_TYPE;
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
if (viewType == CONTENT_TYPE)
View view = inflater.inflate(R.layout.mylist, parent, false);
return new PostViewHolder(view);
else
View view = inflater.inflate(R.layout.ads_row, parent, false);
return new AdViewHolder(view);
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position)
if (getItemViewType(position) == CONTENT_TYPE)
LinkedTreeMap newsList = newsLists.get(position);
if (holder instanceof PostViewHolder)
PostViewHolder viewholder = (PostViewHolder) holder;
viewholder.textView.setText(newsList.get("title").toString());
if (!newsList.get("image").toString().isEmpty())
viewholder.imageView.setVisibility(View.VISIBLE);
Picasso.get()
.load(newsList.get("image").toString())
.into(viewholder.imageView);
else
viewholder.imageView.setVisibility(View.GONE);
else if (getItemViewType(position) == AD_TYPE && holder instanceof AdViewHolder)
((AdViewHolder) holder).loadAd();
【讨论】:
获取空指针异常尝试在空对象引用上调用虚拟方法“void android.view.ViewGroup.addView(android.view.View)” 确保ads_row.xml
包含一个id为myList_fb_banner
的线性布局。
谢谢。广告现在是可见的..但我面临的问题是在我的应用程序中我有 4 个片段..现在发生的情况是,当广告在片段一中可见时在其他片段中不可见。
很高兴它现在可以工作了。您的广告在片段之间的可见性是一个不同的问题。您可以在 cmets 中发布问题并在此处添加链接,我会看看。如果此问题中提出的问题得到解决,请接受它并可选择投票。
***.com/questions/63032010/…以上是关于在 recyclerview 中实现 facebook 横幅广告的主要内容,如果未能解决你的问题,请参考以下文章
在 recyclerview 片段中实现上下文操作模式的问题
如何在基于 SQLite 的 recyclerview 中实现 searchview? - 安卓
如何使用 StaggeredGridLayoutManager 在 recyclerview 中实现无限滚动(分页)
Kotlin中实现RecyclerView嵌套RecyclerView