I am trying to upgrade my app from listview to recyclerview. When I was using listview I had embedded ads within the listview using this tutorial.

I am not able to add it within recyclerview similarly. Any views on how this is to be done in a Recyclerview?

Currently in my listview the code is as below for loading ads:

    if ((position % k) == 0) {
      if (convertView instanceof AdView) {
        return convertView;
      } else {
        // Create a new AdView
        AdView adView = new AdView(activity, AdSize.BANNER,
                                   ADMOB_ID);

        float density = activity.getResources().getDisplayMetrics().density;
        int height = Math.round(AdSize.BANNER.getHeight() * density);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(
            AbsListView.LayoutParams.FILL_PARENT,
            height);
        adView.setLayoutParams(params);

        adView.loadAd(new AdRequest());
        return adView;
      }
    } else {
      return delegate.getView(position - (int) Math.ceil(position / k) - 1,
          convertView, parent);
    }

This is how it should look:

ListView Items

Update: Refer this video from google, it gives the complete explanation


Solution 1:

In your adapter, you first need to override getItemViewType, for example:

@Override
public int getItemViewType(int position) 
{
    if (position % 5 == 0)
        return AD_TYPE; 
    return CONTENT_TYPE;
}

Then in onCreateViewHolder, inflate a different view according to the type. Something like this:

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) 
{
    View v = null;

    if (viewType == AD_TYPE)
    {
        v = new AdView(activity, AdSize.BANNER, ADMOB_ID);
        float density = activity.getResources().getDisplayMetrics().density;
        int height = Math.round(AdSize.BANNER.getHeight() * density);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT,height);
        v.setLayoutParams(params);

     AdRequest adRequest = new AdRequest.Builder().build();
        if (adRequest != null && v != null){
            v.loadAd(adRequest);
         }
    }
    else 
        v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item_layout, viewGroup, false);

    RecyclerView.ViewHolder viewHolder = new RecyclerView.ViewHolder(v);
    return viewHolder;
}

Solution 2:

How to add AD list items and don't skip content items and don't insert null items to content list?

private static final int LIST_AD_DELTA = 3;
private static final int CONTENT = 0;
private static final int AD = 1;

@Override
public int getItemViewType(int position) {
    if (position > 0 && position % LIST_AD_DELTA == 0) {
        return AD;
    }
    return CONTENT;
}

@Override
public BaseRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == CONTENT) {
        return new ContentRecyclerHolder(parent, layoutId) {
            @Override
            protected void onCardClick(CardView card) {
                fragmentManager.showPagerFragmentWithTransition(card.getContext(), getRealPosition(getAdapterPosition()));
            }
        };
    } else {
        return new AdRecyclerHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_ad, parent, false));
    }
}

@Override
public int getItemCount() {
    int additionalContent = 0;
    if (data.size() > 0 && LIST_AD_DELTA > 0 && data.size() > LIST_AD_DELTA) {
        additionalContent = data.size() / LIST_AD_DELTA;
    }
    return data.size() + additionalContent;
}

@Override
public void onBindViewHolder(BaseRecyclerHolder baseHolder, int position) {
    if (getItemViewType(position) == CONTENT) {
        ContentRecyclerHolder holder = (ContentRecyclerHolder) baseHolder;
        Content content = data.get(getRealPosition(position));
    } else {
        AdRecyclerHolder holder = (AdRecyclerHolder) baseHolder;
            AdRequest adRequest = new AdRequest.Builder().build();
            if (adRequest != null && holder.adView != null){
                holder.adView.loadAd(adRequest);
             }
    }
}

private int getRealPosition(int position) {
    if (LIST_AD_DELTA == 0) {
        return position;
    } else {
        return position - position / LIST_AD_DELTA;
    }
}

Solution 3:

In your ArrayList, add null values to positions you want to show ads

    for(int i=0;i<arr.size();i++){
       if(i%5==0)
       {
         alist.add(null);
       }
       alist.add(arr.get(i));
    }

Then in getItemViewType(),

  @Override
public int getItemViewType(int position) {
   if(qlist.get(position)==null)
       return AD_TYPE;
   return CONTENT_TYPE;
}

Then,

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = null;
    ViewHolder vh=null;
    if (viewType == AD_TYPE)
    {
        v=inflater.inflate(R.layout.adview_item, parent, false);
        vh=new AdviewHolder(v);

    }
    else {
        v = inflater.inflate(R.layout.cardview_items, parent, false);
        vh = new ContentViewHolder(v);
    }
    return vh;
}

This will not skip any items but will insert ads in to required positions.

Solution 4:

I'm using retrofit with gson converter and need to display ads after every 5 items . By using above all answer,I was getting wrong position for item . So I've insert custom value to every 5th element in json response .like this

call.enqueue(new Callback<List<DataModel>>() {
            @Override
            public void onResponse(Call<List<DataModel>> call, Response<List<DataModel>> response) {

                if (response.body() != null) {

                    List<DataModel> list_Data = response.body();
                    List<DataModel> list_ad_Data = new ArrayList<>();

                        for (int i = 0; i < list_Data.size(); i++) {
                            if (i %5 == 0 ){
                                list_ad_Data.add(null);
                            }
                                list_ad_Data.add(list_Data.get(i));
                        }

                        adapter = new HomeAdapter(getActivity(), list_ad_Data);

In my Adapter. I've changed viewtype according to

@Override
    public int getItemViewType(int position) {
        if (list_data.get(position) == null) {
            return AD;
        }
        return CONTENT;

    }

changing viewtype is already mentioned in above answers. Hope it helps to future readers.