繁体   English   中英

Adview(admob)与异步数据加载

[英]Adview (admob) with asynchronous data loading

我有一个活动,其中广告填充在从java代码填充的动态表中,此表具有从网络异步加载的图像。 当我不包含广告时,一切都按照我的预期运作。 但是,当我将adview以及从网络异步加载的图像包含在内时,它会挂起UI,直到从网络加载图像为止。

我无法理解这种行为,你能不能给我解决方案,其中图像应与adview异步加载。

以下是我的活动代码:

<?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>

谢谢

爱玛

也许您需要从主要的LinearLayout中分离adView,而不是将其加载到将要完成网络(异步)活动的线性布局中。

只需尝试使用框架布局,其中adView位于顶部,margin_top为“55dp”,用于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>

请让我知道状态。

我在Admob原生广告中遇到了类似的问题。 我想在回收商视图中放置广告。 滚动浏览,当我点击原生广告时,用户界面会冻结,只有广告加载后滚动才会继续

我做了一些计时,发现loadAd()需要大约1400ms,后续调用loadAd()的时间减少到150ms,所以我的修复就是在创建片段之后创建一个原生广告:

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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