AdMob广告在回收者视图中无法正确显示

时间:2019-08-31 06:57:40

标签: android android-recyclerview admob

我正在尝试在回收站视图中实施admob横幅广告我想在每个可被2整除的回收站视图项目之后展示横幅广告我的问题是当我尝试实施条件广告在第二位置展示但替换了回收站视图项目时从那个地方为什么回收站视图项目没有显示在显示广告的特定位置。

这是我到目前为止所做的:

public class HomeBookAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

private static final int LAYOUT_ADS= 0;
private static final int LAYOUT_BOOKS= 1;

private ArrayList<LoadHomeBooks> list;
private Context context;

AdRequest adRequest;

public HomeBookAdapter(ArrayList<LoadHomeBooks> list,Context context){

    this.list = list;
    this.context = context;
}

@Override
public int getItemViewType(int position) {

    if(!(position == 0) && position % 2 == 0){

        return LAYOUT_ADS;
    }else{

        return LAYOUT_BOOKS;
    }
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view =null;
    RecyclerView.ViewHolder viewHolder = null;

    if(viewType==LAYOUT_ADS)
    {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adview_layout,parent,false);
        viewHolder = new AdViewHolder(view);
    }
    else
    {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_book_row,parent,false);
        viewHolder= new BookViewHolder(view);
    }

    return viewHolder;

}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

    if(holder.getItemViewType() == LAYOUT_ADS){

        AdViewHolder adHolder = (AdViewHolder)holder;

        adRequest = new AdRequest.Builder().build();
        adHolder.mAdView.loadAd(adRequest);

    }else{

        BookViewHolder bookHolder = (BookViewHolder)holder;

        LoadHomeBooks model = list.get(position);

        final String bookId = model.getbUid();
        final String bookName = model.getbName();
        final String bookImage = model.getbImage();
        final String bookSub = model.getbSub();
        final String bookClass = model.getbClass();
        //User Id
        final String bId = model.getbId();

        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(R.drawable.openbook);

        Glide.with(context).load(model.getbImage()).apply(requestOptions).into(bookHolder.homeBookImage);

        bookHolder.homeBookName.setText(model.getbName());
        bookHolder.homeSubject.setText(model.getbSub());
        bookHolder.homeClass.setText(model.getbClass());

      }
}

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

public class AdViewHolder extends RecyclerView.ViewHolder {

    AdView mAdView;

    public AdViewHolder(@NonNull View itemView) {
        super(itemView);

        mAdView = itemView.findViewById(R.id.mAdView);
    }
}

public class BookViewHolder extends RecyclerView.ViewHolder {

    TextView homeBookName,homeSubject,homeClass;
    ImageView homeBookImage;

    public BookViewHolder(@NonNull View itemView) {
        super(itemView);

        homeBookName = itemView.findViewById(R.id.homeBookName);
        homeSubject = itemView.findViewById(R.id.homeSubject);
        homeClass = itemView.findViewById(R.id.homeClass);
        homeBookImage = itemView.findViewById(R.id.homeBookImage);

    }
  }
} 

请让我知道我做错了什么,或者需要进行哪些更改才能获得理想的结果。任何帮助将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:6)

我认为您应该使用原生广告而不是横幅广告,因为横幅广告无法像这样实施,因为它会违反Google Admob政策。

对于上面的代码,一切看起来不错,也许您应该检查logcat以获得更多信息,或者:

“可能是您最近才创建了一个新的广告单元ID,并请求投放实时广告。在这种情况下,可能要花几个小时才能开始投放广告。如果您正在接收测试广告,则您的实施即可。只需等待几个小时,然后查看您是否可以接收实时广告。否则,可以将您的广告单元ID发送给我们,以供我们调查。”

所以基本上您需要等待几个小时。