简体   繁体   中英

Adview (admob) with asynchronous data loading

I have an activity where the ads are populated over the dynamic table populated from java code, this table has images which are loaded from network asynchronously. When I don't include ad everything works as expected for me. But when I include adview as well the images which loads from network asynchronously it hangs UI until the images are loaded from network.

I could not understand the behavior, Can you please give me solution where the images should load asynchronously along with adview.

Below is my activity code :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/linearLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
     >

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

    <ScrollView
        android:id="@+id/scroll1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TableLayout
            android:id="@+id/table_layout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <TableRow>

                <TextView
                    android:id="@+id/left_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0"
                    android:gravity="left|center_vertical"
                    android:padding="5dip"
                    android:text="Code" />

                <TextView
                    android:id="@+id/middle_text"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="right|center_vertical"
                    android:padding="5dip"
                    android:text="Name of Company" />

                <TextView
                    android:id="@+id/right_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0"
                    android:gravity="right|center_vertical"
                    android:padding="5dip"
                    android:text="1.3" />
            </TableRow>
        </TableLayout>
    </ScrollView>

</LinearLayout>

Thanks

Amandeep

Maybe you need to sepearte the adView from your main LinearLayout, instead of loading it inside the Linear Layout where your network(async) activities will will be done.

Just try using the Frame Layout, with adView at top & with margin_top of "55dp" for the LinearLayout.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top">

<com.google.ads.AdView xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/main_adView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:gravity="center"
    ads:adUnitId = "******"
    ads:adSize = "BANNER"
    >

</com.google.ads.AdView>
</LinearLayout>

<LinearLayout 
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:orientation="vertical">


<ScrollView
    android:id="@+id/scroll1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableLayout
        android:id="@+id/table_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TableRow>

            <TextView
                android:id="@+id/left_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:gravity="left|center_vertical"
                android:padding="5dip"
                android:text="Code" />

            <TextView
                android:id="@+id/middle_text"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="right|center_vertical"
                android:padding="5dip"
                android:text="Name of Company" />

            <TextView
                android:id="@+id/right_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:gravity="right|center_vertical"
                android:padding="5dip"
                android:text="1.3" />
        </TableRow>
    </TableLayout>
</ScrollView>

</LinearLayout>
</FrameLayout>

Please let me know the status .

I had a similar issue with Admob native ads. I wanted to place an ad in a recycler view. Scrolling through, the UI would freeze when I hit the native ad and scrolling would only continue once the ad had loaded

I did some timings and found that loadAd() will take around 1400ms, subsequent calls to loadAd() the time came down to 150ms, so my fix was to create a native ad just after my fragment has been created:

private static boolean preLoadAd = true;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_help, container, false);
    cardsAdapter = new CardsAdapter(Cards.cards, this);
    recyclerView = (RecyclerView) rootView.findViewById(R.id.card_collection);

    //Pre-load ad to allow smoother scrolling
    //First time loading a native ad takes 800-1500ms, afterwards around 150ms
    if (preLoadAd) {
        //only do this once
        preLoadAd=false;
        //add the task to the message cue to run after the help fragment has shown
        recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    CardVariations.createNativeAdViewHolder(recyclerView);
                }catch(Exception e){e.printStackTrace();}
            }},200L);
    }
    return rootView;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM