Hi Lisa,
I would suggest an alternative here. Instead of using setIsRecyclable(), you can try this -
So let's say you have ViewHolder for just an AdView. Have an empty one and do not add the PublisherAdView here like this -
public static class AdViewHolder extends RecyclerView.ViewHolder {
public AdViewHolder(View view) {
super(view);
}
}
Next, in your list of RecyclerView Item's class, have a reference to the PublisherAdView to store it for reusing it. The idea is to externalize the caching process to show the AdView again when need. For example, your Item class can have be something like this -
public class Item {
protected String text;
protected int viewType;
protected Integer id;
private static Integer ID = 1;
// Add this flag and AdView reference
public Boolean newRequest;
public PublisherAdView mAdView;
//
public Item() {
this.id = ID++;
}
}
Finally, in your onBindViewHolder(), for your AdView view type, have something like this -
case 1:{
AdViewHolder viewHolder = (AdViewHolder) holder;
LinearLayout linearLayout = (LinearLayout)viewHolder.itemView.findViewById(R.id.adViewContainer);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
PublisherAdView mAdView = new PublisherAdView(mContext);
linearLayout.removeAllViews();
if (item.newRequest) {
mAdView.setAdUnitId(mContext.getResources().getString(R.string.banner_ad_unit_id));
mAdView.setAdSizes(AdSize.BANNER);
PublisherAdRequest adRequest = new PublisherAdRequest.Builder().build();
mAdView.loadAd(adRequest);
item.newRequest = false;
item.mAdView = mAdView;
} else {
mAdView = item.mAdView;
}
linearLayout.setLayoutParams(llp);
ViewGroup parent = (ViewGroup)mAdView.getParent();
if (parent != null) {
parent.removeView(mAdView);
}
linearLayout.addView(mAdView);
break;
}
It is a manual way to do this but it would ensure that you make only one AdRequest only. Let us know if you need anything else.
Thanks,
Arjun Busani
Mobile Ads SDK Team