如何在回收者视图android中放置Admob本机高级广告? [英] How to place Admob Native Advanced Ads in recycler view android?

查看:153
本文介绍了如何在回收者视图android中放置Admob本机高级广告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将admob原生高级广告放置在Android应用程序中回收者"视图的每个3个位置.

I want place the admob native advanced ads in every 3 position of my recycler view in android app.

我想使用Admob提供的模板.

I would like to template provided by Admob.

https://github.com/googleads/googleads-mobile-android-native-templates

这是原生广告的xml代码实现

Here is xml code implementation of native ads

<com.google.android.ads.nativetemplates.TemplateView
android:id="@+id/my_template"
<!-- this attribute determines which template is used. The other option is
@layout/gnt_medium_template_view -->
app:gnt_template_type="@layout/gnt_small_template_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />

这是Admob的Java代码实现

Here is Java code implementation of Admob

   MobileAds.initialize(this, "[_app-id_]");
   AdLoader adLoader = new AdLoader.Builder(this, "[_ad-unit-id_]")
 .forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
   @Override
   public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
      NativeTemplateStyle styles = new
          NativeTemplateStyle.Builder().withMainBackgroundColor(background).build();

      TemplateView template = findViewById(R.id.my_template);
      template.setStyles(styles);
      template.setNativeAd(unifiedNativeAd);

    }
 })
 .build();

  adLoader.loadAd(new AdRequest.Builder().build());
 }

RecyclerView适配器类:

public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.MyViewHolder>{
private Context mContext;
private List<ArticleJson> articleList;
String titleoflist;

public class MyViewHolder extends RecyclerView.ViewHolder {
    TextView txtTitle,txtDesc,txtStatus,txtColor,txtAuthor;
    LinearLayout linearLayout;
    private ArticleJson m_articleJson;

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

    public void bindView(final ArticleJson articleJson){
        m_articleJson = articleJson;
        txtTitle.setText(articleJson.getmTitle());
        txtAuthor.setText(titleoflist);
    }
}

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

@NonNull
@Override
public ArticleAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.from(parent.getContext())
          .inflate(R.layout.list_item_article, parent, false);
   return new ArticleAdapter.MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull ArticleAdapter.MyViewHolder holder, int position) {
final ArticleJson articleJson = articleList.get(position);
    holder.bindView(articleJson);
    holder.linearLayout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      //Toast
    }
});
}

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

推荐答案

首先,您创建Ads continer item_ads.xml文件夹

    <com.google.android.ads.nativetemplates.TemplateView
android:id="@+id/my_template"
<!-- this attribute determines which template is used. The other option is
@layout/gnt_medium_template_view -->
app:gnt_template_type="@layout/gnt_small_template_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />

在适配器中插入,您必须将扩展更改为

 extends RecyclerView.Adapter<RecyclerView.ViewHolder> 

现在您需要覆盖 4种方法

公共RecyclerView.ViewHolder onCreateViewHolder

public RecyclerView.ViewHolder onCreateViewHolder

公共无效 onBindViewHolder

public int getItemCount ()

public int getItemCount()

public int getItemViewType

public int getItemViewType

在getItemViwType方法中,我们定义了两种可能性

in the getItemViwType methode we define two possibilities

@Override
    public int getItemViewType(int position) {     
        if (AD_LOGIC_CONDITION)) {
            
            return AD_TYPE;
        }else{

        return CONTENT_TYPE; ///do not forget to initialize bouth of AD_TYPE and   CONTENT_TYPE
    }

然后,我们为您的内容创建两个视图所有者,并为我们的广告创建第二个视图所有者我假设您知道如何创建您的视图持有人,所以我只解释一下AD视图持有人

then we create two view holder one for your content and secend for our Ad I will assume that you know how to create your view holder so i will just explain the AD View holder

class adViewHolder extends RecyclerView.ViewHolder {
        TemplateView Adtemplate;

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

现在我们返回 onCreateViewHolder 方法

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{
            YourViewHolder mYourViewHolder = new YourViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, null, false));
            return mYourViewHolder;
        }

现在我们进入 onBindViewHolder

 @Override
        public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
            if (getItemViewType(position) == TYPE_CONTENT) {
                ///your data 
// AN EXAMPLE
                ((YourViewHolder) holder).textview.setText(data.getmtext());
                ((YourViewHolder) holder).Img.setImageResource(data.getmImg());
                ((YourViewHolder) holder).title.setText(data.getmName());
            } else if (getItemViewType(position) == TYPE_AD){
    
                final AdLoader adLoader = new AdLoader.Builder(context, "ca-app-pub-3940256099942544/2247696110")
                        .forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
                            @Override
                            public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
                                // Show the ad.
                                NativeTemplateStyle styles = new
                                        NativeTemplateStyle.Builder().build();
    
                                TemplateView template = ((adViewHolder) holder).Adtemplate;
                                template.setStyles(styles);
                                template.setNativeAd(unifiedNativeAd);
    
                            }
                        })
                        .withAdListener(new AdListener() {
                            @Override
                            public void onAdFailedToLoad(int errorCode) {
                                // Handle the failure by logging, altering the UI, and so on.
                            }
                        })
                        .withNativeAdOptions(new NativeAdOptions.Builder()
                                // Methods in the NativeAdOptions.Builder class can be
                                // used here to specify individual options settings.
                                .build())
                        .build();
                adLoader.loadAd(new AdRequest.Builder().build());
    
    
            }

这篇关于如何在回收者视图android中放置Admob本机高级广告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆