androidandroid-recyclerviewadmob

AdMob ads are not showing properly in recycler view


I am trying to implement AdMob banner ads in recycler view I want to show banner ads after every recycler view item divisible by 2. My problem is when I am trying to implement condition ad is showing on second position but replaced the recycler view item from that place. Why is the recycler view item not showing on particular position where ads are showing?

This is what I have done so far:

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

    }
  }
} 

What am I doing wrong or what needs to be changed to get desired result?


Solution

  • I think you should use Native ads instead of using Banner because Banner ads can't be implemented like this as it will violate Google Admob policies.

    And for the above code, everything looks fine, maybe you should check your logcat for more info or:

    "It could be that you have only recently created a new Ad Unit ID and requesting for live ads. It could take a few hours for ads to start getting served if that is that case. If you are receiving test ads then your implementation is fine. Just wait a few hours and see if you are able to receive live ads then. If not, can send us your Ad Unit ID for us to look into."

    so basically you have need to wait for a few hours.