title: How can I limit the number of native ads in recyclerview
body: <p>How can I limit the number of native ads in recyclerview
  I only want 5 native ads in first 100 lines in recyclerview (per every 20 lines).
  My recyclerview have got 10000 row. It annoys the user on every 20 lines of ad output. So I want to limit the number of ads to 5. I made a lot of changes, but I could not.
  Thank you in advance for your help</p>
  
  <p><strong>advertising related section in my activity</strong></p>
  
  <pre><code>public static final int ITEMS_PER_AD = 20;
  private static final int NATIVE_EXPRESS_AD_HEIGHT = 100;
  
  private void addNativeExpressAds() {
  
          // Loop through the items array and place a new Native Express ad in every ith position in
          // the items List.
          for (int i = 0; i &lt;= mRecyclerViewItems.size(); i += ITEMS_PER_AD) {
              final NativeExpressAdView adView = new NativeExpressAdView(getActivity());
              mRecyclerViewItems.add(i, adView);
          }
      }
      private void setUpAndLoadNativeExpressAds() {
          mRecyclerView.post(new Runnable() {
              @Override
              public void run() {
                  try {
                      for (int i = 0; i &lt;= mRecyclerViewItems.size(); i += ITEMS_PER_AD) {
                          final NativeExpressAdView adView = (NativeExpressAdView) mRecyclerViewItems.get(i);
                          AdSize adSize = new AdSize(AdSize.FULL_WIDTH, NATIVE_EXPRESS_AD_HEIGHT);
                          adView.setAdSize(adSize);
                          adView.setAdUnitId(AD_UNIT_ID);
                      }
                      loadNativeExpressAd(0);
                  }catch (Exception e){
  
                  }
  
              }
          });
      }
      private void loadNativeExpressAd(final int index) {
  
  //        if (index &gt;= mRecyclerViewItems.size()) {
  //            return;
  //        }
  
          if (index &gt;= mRecyclerViewItems.size()) {
              return;
          }
  
          Object item = mRecyclerViewItems.get(index);
          if (!(item instanceof NativeExpressAdView)) {
              throw new ClassCastException("Expected item at index " + index + " to be a Native"
                      + " Express ad.");
          }
  
          final NativeExpressAdView adView = (NativeExpressAdView) item;
          // Set an AdListener on the NativeExpressAdView to wait for the previous Native Express ad
          // to finish loading before loading the next ad in the items list.
          adView.setAdListener(new AdListener() {
              @Override
              public void onAdLoaded() {
                  super.onAdLoaded();
                  // The previous Native Express ad loaded successfully, call this method again to
                  // load the next ad in the items list.
                  if (index&lt;24){
                  loadNativeExpressAd(index + ITEMS_PER_AD);
                  }
              }
  
              @Override
              public void onAdFailedToLoad(int errorCode) {
                  // The previous Native Express ad failed to load. Call this method again to load
                  // the next ad in the items list.
                  Log.e("MainActivity", "The previous Native Express ad failed to load. Attempting to"
                          + " load the next Native Express ad in the items list.");
                  //loadNativeExpressAd(index + ITEMS_PER_AD);
              }
          });
          try {
              adView.loadAd(new AdRequest.Builder().build());
          }catch (Exception e){}
  
      }
  </code></pre>
  
  <p><strong>and my RecyclerViewAdapter.java</strong></p>
  
  <pre><code>class RecyclerViewAdapter extends FastScrollRecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt;  {
      private static final int MENU_ITEM_VIEW_TYPE = 0;
      private static final int NATIVE_EXPRESS_AD_VIEW_TYPE = 1;
      private final Context mContext;
      private final List&lt;Object&gt; mRecyclerViewItems;
  
      ItemClickListener listener;
      public RecyclerViewAdapter(Context context, List&lt;Object&gt; recyclerViewItems, ItemClickListener listener) {
          this.mContext = context;
          this.mRecyclerViewItems = recyclerViewItems;
          this.listener=listener;
      }
  
      public class MenuItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
          private TextView menuItemName;
          private TextView menuItemDescription;
          private TextView menuItemPrice;
          private TextView menuItemCategory;
          private ImageView menuItemImage;
          ItemClickListener itemClickListener;
  
          MenuItemViewHolder(View view) {
              super(view);
              menuItemImage = (ImageView) view.findViewById(R.id.cinsiyetresmi);
              menuItemName = (TextView) view.findViewById(R.id.menu_item_name);
              menuItemPrice = (TextView) view.findViewById(R.id.menu_item_price);
              menuItemCategory = (TextView) view.findViewById(R.id.menu_item_category);
              menuItemDescription = (TextView) view.findViewById(R.id.menu_item_description);
              view.setOnClickListener(this);
          }
          @Override
          public void onClick(View v) {
              this.itemClickListener.onItemClick(v,getLayoutPosition());
          }
  
          public void setItemClickListener(ItemClickListener ic)
          {
              this.itemClickListener=ic;
  
          }
      }
  
      public class NativeExpressAdViewHolder extends RecyclerView.ViewHolder  {
  
          NativeExpressAdViewHolder(View view) {
              super(view);
          }
  
      }
  
      @Override
      public int getItemCount() {
          return mRecyclerViewItems.size();
      }
  
      @Override
      public int getItemViewType(int position) {
          return (position % isimler_erkek.ITEMS_PER_AD == 0) ? NATIVE_EXPRESS_AD_VIEW_TYPE
                  : MENU_ITEM_VIEW_TYPE;
      }
  
      @Override
      public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
          switch (viewType) {
              case MENU_ITEM_VIEW_TYPE:
                  View menuItemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
                          R.layout.menu_item_container, viewGroup, false);
                  return new MenuItemViewHolder(menuItemLayoutView);
              case NATIVE_EXPRESS_AD_VIEW_TYPE:
                  // fall through
              default:
                  View nativeExpressLayoutView = LayoutInflater.from(
                          viewGroup.getContext()).inflate(R.layout.native_express_ad_container,
                          viewGroup, false);
                  return new NativeExpressAdViewHolder(nativeExpressLayoutView);
          }
  
      }
  
      @Override
      public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
          int viewType = getItemViewType(position);
          MenuItem menuItem;
          MenuItemViewHolder menuItemHolder;
          switch (viewType) {
              case MENU_ITEM_VIEW_TYPE:
                  menuItemHolder = (MenuItemViewHolder) holder;
                  menuItem = (MenuItem) mRecyclerViewItems.get(position);
  
                  // Get the menu item image resource ID.
                  String imageName = menuItem.getImageName();
                  int imageResID = mContext.getResources().getIdentifier(imageName, "drawable",
                          mContext.getPackageName());
  
                  // Add the menu item details to the menu item view.
                  menuItemHolder.menuItemImage.setImageResource(imageResID);
                  menuItemHolder.menuItemName.setText(menuItem.getName());
                  menuItemHolder.menuItemPrice.setText(menuItem.getPrice());
                  menuItemHolder.menuItemCategory.setText(menuItem.getCategory());
  
  
                  menuItemHolder.menuItemDescription.setText(menuItem.getDescription());
                  final  int idsi= Integer.parseInt(menuItem.getDescription());
                  menuItemHolder.setItemClickListener(new ItemClickListener() {
                      @Override
                      public void onItemClick(View v, int pos) {
                          //Snackbar.make(v,String.valueOf(players.get(pos).getId()), Snackbar.LENGTH_SHORT).show();
                          try {
                              listener.onItemClick(v,idsi);
                          }
                          catch (Exception e){
                          }
                          //Toast.makeText(this.getApplicationContext(), "bak", Toast.LENGTH_LONG).show();
                      }
                  });
  
                  break;
              case NATIVE_EXPRESS_AD_VIEW_TYPE:
                  // fall through
              default:
  
                      NativeExpressAdViewHolder nativeExpressHolder =
                              (NativeExpressAdViewHolder) holder;
                      NativeExpressAdView adView =
                              (NativeExpressAdView) mRecyclerViewItems.get(position);
                      ViewGroup adCardView = (ViewGroup) nativeExpressHolder.itemView;
  
                      if (adCardView.getChildCount() &gt; 0) {
                          adCardView.removeAllViews();
                      }
                      if (adView.getParent() != null) {
                          ((ViewGroup) adView.getParent()).removeView(adView);
                      }
  
                      // Add the Native Express ad to the native express ad view.
                      adCardView.addView(adView);
  
          }
      }
  
  }
  </code></pre>