如何将 Admob Native Advanced Ads 放置在回收站视图 android 中? [英] How to place Admob Native Advanced Ads in recycler view android?

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

问题描述

我想将 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 个方法

now you need to override 4 methods

公共 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());
    
    
            }

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

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