Rafael Ruiz Muñoz
Rafael Ruiz Muñoz

Reputation: 5472

Problems loading AdView inside a RecyclerView

I've been looking in Stackoverflow how to integrate an AdView inside a RecyclerView. I've been following these posts:

One, two

Basically the way to do it is calling loadAd inside onCreateViewHolder or inside the constructor of the ViewHolder.

Either way, this is my implementation:

JAVA

public class AdExpressViewHolder extends RecyclerView.ViewHolder {

    public AdExpressViewHolder(View itemView) {
        super(itemView);

        final AdView adView = (AdView)itemView.findViewById(R.id.adView);
        AdRequest request = new AdRequest.Builder()
                .build();
        adView.loadAd(request);
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            ads:adUnitId="**********************"
            ads:adSize="BANNER">
        </com.google.android.gms.ads.AdView>

    </RelativeLayout>
</RelativeLayout>

The problem is: when I scroll the RecyclerView, it seems to load on the UI thread since it gets stuck, only the first time. The rest of the times is ok.

This is the video that demonstrates it:

Video

As you can see, the first one is blocking the UI, but not the second one.

What am I doing wrong?

Thank you in advance.


EDIT

I've tried to load a conventional AdView in an activity, fixed. It works and it doesn't seem to be load in the UI Thread. Seems it's just happening in the RecyclerView.


After 3 weeks, I've done a Method profiling, and this is what I've got out:

You can realise the red spots. Those are 2 different AdView loading, while the rest are 38 normal custom views of mine.

To be more concrete, these are the functions, so it's regarding 100% the AdView:

enter image description here

Upvotes: 4

Views: 1019

Answers (1)

Rafael Ruiz Mu&#241;oz
Rafael Ruiz Mu&#241;oz

Reputation: 5472

It seems a bug of the Ads SDK for Android and it's not been fixed, at least until v9.4.0.

More information here: https://groups.google.com/forum/#!searchin/google-admob-ads-sdk/ui$20thread%7Csort:relevance/google-admob-ads-sdk/k4IFZA_QGT4/3gMHaCPPBQAJ

Upvotes: 2

Related Questions