gpt4 book ai didi

android - RecyclerView android中的Admob原生广告

转载 作者:行者123 更新时间:2023-12-04 12:29:43 35 4
gpt4 key购买 nike

在放置 admob 广告时,我的 Recycler View 遇到了这个问题
问题:它实际上是替换列表中的项目并放置广告
enter image description here
应用堆栈溢出成员之一推荐的此解决方案后,会出现问题
How to place Admob Native Advanced Ads in recycler view android?
这是我正在使用的代码:

@Override
public int getItemViewType(int position) {
if (position>1 && (position+1) % 4 == 0) {

return AD_TYPE;
} else {

return CONTENT_TYPE;
}

}

@Override
public int getItemCount() {
return articleList.size();
}
这是完整的代码
public class ArticleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private Context mContext;
private List<ArticleJson> articleList;
String titleoflist;
private static final int AD_TYPE = 2;
private static final int CONTENT_TYPE = 1;

public class MyViewHolder extends RecyclerView.ViewHolder {
TextView txtTitle;
LinearLayout linearLayout;
private ArticleJson m_articleJson;

public MyViewHolder(View view) {
super(view);
txtTitle = view.findViewById(R.id.texViewArticleTitle);

}

public void bindView(final ArticleJson articleJson){
m_articleJson = articleJson;

}
}

class adViewHolder extends RecyclerView.ViewHolder {
TemplateView Adtemplate;
UnifiedNativeAdView unifiedNativeAdView;

public adViewHolder(@NonNull View itemView) {
super(itemView);
Adtemplate = itemView.findViewById(R.id.mednative_placeholder1);
unifiedNativeAdView = itemView.findViewById(R.id.native_ad_view);
}
}

public ArticleAdapter(Context mContext, List<ArticleJson> articleList,String titleoflist) {
this.mContext = mContext;
this.articleList = articleList;
this.titleoflist = titleoflist;
}


@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == AD_TYPE) {
adViewHolder madViewHolder = new adViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ads, null, false));
return madViewHolder;
} else{
MyViewHolder mYourViewHolder = new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_article, null, false));
return mYourViewHolder;
}
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) == CONTENT_TYPE) {
final ArticleJson articleJson = articleList.get(position);
((MyViewHolder)holder).bindView(articleJson);
((MyViewHolder)holder).linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

}
});

}else if (getItemViewType(position) == AD_TYPE){

MobileAds.initialize(mContext, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {}
});

AdLoader adLoader2 = new AdLoader.Builder(mContext, "ca-app-pub-8376062614303044/3237856178")
.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
@Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
UnifiedNativeAdView unifiedNativeAdView = ((adViewHolder) holder).unifiedNativeAdView;
unifiedNativeAdView.setVisibility(View.VISIBLE);
NativeTemplateStyle styles = new
NativeTemplateStyle.Builder().build();

TemplateView template = ((adViewHolder) holder).Adtemplate;
template.setStyles(styles);
template.setNativeAd(unifiedNativeAd);


}
})
.build();

adLoader2.loadAd(new AdRequest.Builder().build());



}
}

@Override
public int getItemCount() {
return articleList.size();
}

@Override
public int getItemViewType(int position) {
if (position>1 && (position+1) % 4 == 0) {

return AD_TYPE;
} else {

return CONTENT_TYPE;
}

}
}
public class ArticleJson {
private String mid;
private String mTitle;
boolean isAd = false;

public ArticleJson() {

}
public ArticleJson(String mid, String mTitle) {
this.mTitle = mTitle;
this.mid = mid;
}

public String getMid() {
return mid;
}

public String getmTitle() {
return mTitle;
}}

最佳答案

出现问题是因为您只使用适配器中的原始数据列表,而没有将广告对象放入列表中。因此,根据您上面的示例,您将只有 10 个项目(原始数据)而不是 13 个(带有广告对象)。
要在不影响原始数据的情况下达到您想要的结果,您可以执行以下操作:
1 .将您的 ArticleAdapter 构造函数更改为如下所示。在这里,我创建了一个新的 ArticleJson 列表(articlesWithAdsList),其中包含广告和项目,并且我使用 将每个广告放置在每 3 行中。广告位 (广告频率数)变量,您可以根据需要进行更改。为了区分广告和项目,我添加了一个 bool 标志 isAd 在 ArticleJson 对象中,最后您的 articleList 指向新创建的articlesWithAdsList。

public ArticleAdapter(Context mContext, List<ArticleJson> articleList, String titleoflist) {
this.mContext = mContext;
//prepare a new List<ArticleJson> having both ads and data
List<ArticleJson> articlesWithAdsList = new ArrayList<>();
if(articleList!=null && articleList.size()>0)
{
int adPos = 0;
//iterate through the actual data list and prepare the new articlesWithAdsList
for (int i = 0; i < articleList.size(); i++)
{
//initialize and add the ad in every 3rd row
if(adPos == 3) {
ArticleJson advertise = new ArticleJson();
advertise.isAd = true;
articlesWithAdsList.add(advertise);
adPos = 0;
}
//and add the actual Item ArticleJson object
ArticleJson articleJson = articleList.get(i);
articleJson.isAd = false;
articlesWithAdsList.add(articleJson);
adPos++;
}
}
//articleList now have both ads and data items
this.articleList = articlesWithAdsList;
this.titleoflist = titleoflist;
}
2 .更改 getItemViewType(int position) 使用 isAd 用于区分 AD_TYPE 和 Item CONTENT_TYPE 的标志,如下所示:
@Override
public int getItemViewType(int position) {
ArticleJson articleJson = articleList.get(position);
if (articleJson.isAd) {
return AD_TYPE;
} else {
return CONTENT_TYPE;
}
}
3 .最后加上 isAd 标记到您的 ArticleJson 对象中:
public class ArticleJson {
boolean isAd = false;
public ArticleJson() {

}
}

关于android - RecyclerView android中的Admob原生广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66580445/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com