在 recyclerview 中实施 facebook 横幅广告

Implement facebook banner ads in recyclerview

我正在尝试在 recyclerview 的第 3 个位置实施 Facebook 横幅广告(不是原生横幅广告)。我创建了一个具有广告布局的 ads_row.xmlads_row.xml 显示正常但广告不可见。我将 post 下面是我的适配器代码。

如果有人可以就我应该使用哪种方法在 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 为空,您的应用程序将崩溃。如果对象为空,则不能操作该对象的属性:
if (adView == null){
    adView.setVisibility(View.GONE);
}

您很可能希望完全删除此声明。

  1. 位置计算错误。您是说要在回收站视图项目的第三行加载广告。这个表达式 newsLists.size() - 3 不会给你第三行的索引:
if (position == newsLists.size()-3) {
    adView.setVisibility(View.VISIBLE);
    if (adView != null){
        adView.loadAd();
    }
}

第三行的索引是总是 2.索引的枚举从0开始,作为第一个索引。

将这段代码更新为:

if (position == 2) {
    adView.loadAd();
}
  1. 没有 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();
        }
    }
}