简体   繁体   English

在 recyclerview 中实施 facebook 横幅广告

[英]Implement facebook banner ads in recyclerview

I am trying to implement an Facebook banner ad(not native banner) on 3rd position of the recyclerview.我正在尝试在 recyclerview 的第 3 个 position 上实施 Facebook 横幅广告(不是本机横幅)。 I have created an ads_row.xml which has the ad layout.我创建了具有广告布局的ads_row.xml ads_row.xml is displayed properly but ad is not visible. ads_row.xml显示正确,但广告不可见。 I will post my adapter code below.我将在下面发布我的适配器代码。

I would appreciate if someone could give me advice about which approach i should use in my case to show ads in recyclerview.如果有人能给我建议我应该使用哪种方法在 recyclerview 中展示广告,我将不胜感激。

Happy Coding快乐编码

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

    }
}

A few issues I noticed.我注意到的几个问题。

  1. Your app will crash if adView is null.如果adView为 null,您的应用将崩溃。 You cannot manipulate attributes of an object if it is null:如果 object 是 null,则不能操作其属性:
if (adView == null){
    adView.setVisibility(View.GONE);
}

Most likely you want this statement removed totally.您很可能希望完全删除此语句。

  1. Position calculation is wrong. Position 计算错误。 You are saying that you want to load an ad in the third row of a recycler view items.您是说要在回收商视图项目的第三行加载广告。 This expression newsLists.size() - 3 won't give you the index of a third row:这个表达式newsLists.size() - 3不会给你第三行的索引:
if (position == newsLists.size()-3) {
    adView.setVisibility(View.VISIBLE);
    if (adView != null){
        adView.loadAd();
    }
}

Index of the third row is always 2. Enumeration of indices starts from 0 as the first index.第三行的索引始终为 2。索引的枚举从 0 作为第一个索引开始。

Update this piece of code to be:将这段代码更新为:

if (position == 2) {
    adView.loadAd();
}
  1. There is no getItemViewType method override.没有getItemViewType方法覆盖。 Or at least it is not the question.或者至少这不是问题。

I've updated your adapter.我已经更新了你的适配器。 You can try to copy-paste it into your application to check how it's working.您可以尝试将其复制粘贴到您的应用程序中,以检查它是如何工作的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM